LoginSignup
49
46

More than 5 years have passed since last update.

[Swift] Storyboardを使わずに画面遷移をすると、遷移先が黒画面になるときやstoryboard doesn't contain a view controller with identifier 'って言われるとき。

Posted at

iOSアプリで画面遷移を行う方法はたくさんあります。storyboardでやったり、コードでやったり。

例えば
// set next VC
let nextVC: UIViewController = FooVC()

// set animation
nextVC.modalTransitionStyle = UIModalTransitionStyle.PartialCurl

// transition
self.presentViewController(nextVC, animated: true, completion: nil)  
navigationありのとき
let nextVC = FooVC() as UIViewController
nextVC.modalTransitionStyle = UIModalTransitionStyle.PartialCurl
navigationController?.pushViewController(nextVC, animated: true)

遷移先画面が黒くなるとき

Screen Shot 2015-08-13 at 11.50.26.png

Storyboardでのレイアウトが反映されず、真っ黒な画面が出てしまうのは、コードでの遷移の際はStoryboardを明示しないと、コードでレイアウトを作っているものとして扱われる(いちいちstoryboardをよまない)っぽい。。。

なので、以下の様にしてやるといいです。

storyboardでレイアウトは作成していて、遷移はコードでやろうとしたときとか、Storyboardをまたがる遷移のとき。

let storyboard = UIStoryboard(name: "STORYBOARD_NAME", bundle: nil)
let nextVC = storyboard.instantiateViewControllerWithIdentifier("STORYBOARD_ID_OF_VC_CLASS") as! UIViewController
navigationController?.pushViewController(nextVC, animated: true)

エラー storyboard doesn't contain a view controller with identifier ''

ここまでして、

storyboard doesn't contain a view controller with identifier 'foo''

とかエラーがでたりしたら、
それはstoryboard idの設定をミスっているのかもしれないです。

特に use storyboard idにチェックを入れていないことがあったりします。

Screen Shot 2015-08-13 at 11.27.22.png

References

49
46
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
49
46