IOS - setting "rootViewController" results in black screen on device only

With a very simple single view app, I deleted the main storyboard file and removed any references to it. So I set the rootViewController window programmatically. However, as long as this displays the only view (containing the label) correctly in the simulator, it displays a black screen when it is run on the device. Here's just the code for the app.

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = DummyViewController()
        window?.makeKeyAndVisible()
        return true
    }

      

I removed the entry for the main storyboard from the info.plist file, as well as the "Main Interface" entry in the general settings.

I am using Swift 3 and targeting an iOS 8 device. I am using XCode 8.3.1.

There is no output in the console and there are no exceptions. The viewDidLoad function even fires at a breakpoint, so the code seems to work correctly.

Any ideas?

Below is the bones code for the DummyViewController as requested.

class DummyViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

      

General settings not showing links to the main interface enter image description here

Here is an image for the .xib linked to DummyViewController enter image description here

** The solution to get around this is to manually specify the .xib to load for the DummyViewController **

+5


source to share


3 answers


It looks like the ViewController is not set to display anything. Unless you are using xib (in which case you will need to load the view controller differently, see below), there is nothing describing how the ViewController should look like.

To test this, you can add a line self.view.backgroundColor = UIColor.red

to the ViewController method viewDidLoad()

and then run it again on the device if the background color turns red, then hurray! The next step is to programmatically add the UILabel.


Loading UIViewController from XIB



let vc = MyViewController(nibName: "xibname", bundle: nil)

Alternatively, you can mask the boot by adding a custom init inside MyViewController

:

class MyViewController: UIViewController {
    required init() {
        super.init(nibName: "xibname", bundle: nil)
    }
}

      

(Thanks zonily-jame for adding the hide to the class)

+2


source


change the root of the window as and set the color

let viewController:DummyViewController = DummyViewController()
self.window?.backgroundColor = UIColor.white
self.window?.rootViewController = viewController

      



And change your controller

  override func viewDidLoad() {
    self.view.backgroundColor = UIColor.white
  }

      

+1


source


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

         let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
         let loginView : BaseClassVC = mainStoryboardIpad.instantiateViewControllerWithIdentifier("BaseClassVC") as BaseClassVC
            let navigationController = UINavigationController(rootViewController: loginView)

         self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
         self.window?.rootViewController = navigationController
         self.window?.makeKeyAndVisible()

         return true
    }

      

+1


source







All Articles