6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

self.window?.rootViewController の設定でエラー

Posted at

画面真っ暗でエラーが出てしまったのでメモ

Storyboardを使っている場合は注意

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        // ViewControllerを生成する.
        let myFirstViewController: FirstViewController = FirstViewController()
        
        // UIWindowを生成する.
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        // rootViewControllerにNatigationControllerを設定する.
        self.window?.rootViewController = myFirstViewController
        
        self.window?.makeKeyAndVisible()
        
        return true
    }

この場合新しいインスタンスをrootViewControllerにしてしまうが

self.window?.rootViewController = myFirstViewController

Storyboardをルートビューにしたいので下記のように書く
※ViewControllerには
IDとして"ViewController"を指定しておく必要がある。

var storyboard:UIStoryboard =  UIStoryboard(name: "Main",bundle:nil)

self.window?.rootViewController = storyboard.instantiateViewControllerWithIdentifier("ViewController") as UIViewController
6
4
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
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?