うちのいぬ Tech Blog

Tech Blog of Uchinoinu/My dog

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

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

// set next VC
let nextVC: UIViewController = FooVC()
        
// set animation
nextVC.modalTransitionStyle = UIModalTransitionStyle.PartialCurl
        
// transition
self.presentViewController(nextVC, animated: true, completion: nil)  
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