storyboardを手動で画面遷移

遷移先に移る方法
func tapBtn(sender: UIButton) {
    let nextView = NextViewController()
    nextView.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
    self.presentViewController(nextView, animated: true, completion: nil)
}


戻る方法
func tapBtn(sender: UIButton) {
    dismissViewControllerAnimated(true, completion: nil)
}

別な方法も
func next(view: UIViewController, resourceId: String) {
    var next : AnyObjext! = view.storyboard!.instantiateViewControllerWithIdentifier(resourceId)
    (next as! UIViewController).modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
    view.presentViewController(next as! UIViewController, animated: true, completion: nil)
}

呼び方
next(self, resourceId: "vNext")
※StoryBoardで、UIViewControllerにClassとModuleとStoryboard IDの3つを設定済みであること

  • viewDidLoadで呼び出すと以下のワーニング発生する
「Presenting view controllers on detached view controllers is discouraged」

以下で回避可能
self.view.window?.rootViewController!.presentViewController(nextView!, animated: true, completion: nil)

もしくはviewDidLoadで呼ばないで以下で呼ぶようにする。こっちが正解か?
afterXXXLoad

2015/8/18
最終更新:2015年09月04日 11:52