Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Setting rootViewController not working?

Tags:

ios

swift

I'm trying to start a new Swift project. This is my first time trying to create the views programatically. However it doesn't even look like my controller is being loaded? All I see is the launch screen and then a black screen when I load it onto the simulator.

This is my AppDelegate:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?

  func application(application: UIApplication,
      didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    NSLog("zrrrzz") // <------------------------------ Prints properly
    self.window?.rootViewController = self.rootViewController()
    return true
  }

  private func rootViewController() -> UIViewController {
    NSLog("zzz") // <---------------------------------- Does not print ????
    return MapViewController.init()
  }

}

MapViewController:

import UIKit

class MapViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
        label.center = CGPointMake(160, 284)
        label.textAlignment = NSTextAlignment.Center
        label.text = "I am a test label"
        self.view.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(label)
        NSLog("heyyyy!!") //<------------------------------ Also doesn't print ??
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

Am I missing a step? I don't see any warnings / errors when I start the simulator

like image 220
bigpotato Avatar asked Sep 09 '25 10:09

bigpotato


1 Answers

In the line:

self.window?.rootViewController = self.rootViewController()

If the window property is nil, it will not execute your self.rootViewController() call. You can read more about calling methods with optional chaining in the documentation for details.

If you are trying to create your initial user interface in code, you will need to create a UIWindow instance and assign it to self.window. This is done for you automatically when using a Storyboard.

Disclaimer: I haven't written this code in a few versions of iOS, so this may not be exactly correct, but it will get you going in the right direction:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let applicationFrame = UIScreen.mainScreen().applicationFrame
    let window = UIWindow(frame: applicationFrame)
    window.rootViewController = self.rootViewController()
    window.makeKeyAndVisible()
    self.window = window

    return true
}
like image 152
Charles A. Avatar answered Sep 11 '25 00:09

Charles A.