8

With a very basic single view application, I've deleted the main storyboard file and removed any references to it. As such I'm setting the window rootViewController programmatically. However, while this displays the single view (containing a label) correctly in simulator, it displays a black screen when running it on device. Here is the only 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've removed the entry for Main storyboard from the info.plist file, as well as the 'Main Interface' entry in the General settings.

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

There is no output in the console, and there are no exceptions. The viewDidLoad function is even triggering on breakpoint, so the codepath seems to be running correctly.

Any ideas?

Here's the bare bones code for DummyViewController upon request.

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

General settings showing no reference to Main Interface enter image description here

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

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

mbradber
  • 489
  • 5
  • 17

4 Answers4

3

It looks like the ViewController is not set to display anything. Unless you are using a xib (in which case you would need to load the view controller in a different way, see below), there is nothing describing how the ViewController's view should render.

To test this out, you can add the line self.view.backgroundColor = UIColor.red to the ViewController's viewDidLoad() method, then run it again on the device- if the background color turns red, then hooray! The next step will be programmatically adding a UILabel.


Loading UIViewController From a XIB

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

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

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

(Thank you zonily-jame for the addition of hiding it in the class)

Community
  • 1
  • 1
Carter
  • 3,053
  • 1
  • 17
  • 22
  • @mbradber Have you deleted the reference to the main interface in the General settings? – Carter Apr 17 '17 at 06:41
  • @mbradber It doesn't look like your ViewController is set to display anything, so a black screen would be expected.. please see my updated answer. Does the background turn red? – Carter Apr 17 '17 at 06:43
  • So that did work, I see a red background. However, I have a DummyViewController.xib with a label and blue background that is not being rendered on device, as if the original .xib is just being ignored. On simulator the .xib loads and displays correctly. – mbradber Apr 17 '17 at 06:49
  • 2
    Ah okay, so you are using a xib. It looks like what is happening is the xib is not being linked to the ViewController when you render on the device (but it is in the simulator- weird). You could try Clean to try and get the same behavior on both devices. – Carter Apr 17 '17 at 06:52
  • Also try to delete your derived data sometimes it messes up your ide – Zonily Jame Apr 17 '17 at 06:54
  • @Carter Yea, the File's Owner of the Xib is correctly set to DummyViewController, but the Xib is just not being picked up when running on device. I've tried cleaning the project and re-installing app. I'll try loading the DummyViewController and manually loading the .xib. Thanks for your suggestions :) – mbradber Apr 17 '17 at 06:55
  • @Carter Manually specifying the .xib to load makes it work on both device and simulator correctly. Thanks again. – mbradber Apr 17 '17 at 06:58
  • 2
    @mbradber Glad we were able to figure it out :) @ZonilyJame Thanks for the edit, I like the idea of abstracting it in the `UIViewController` – Carter Apr 17 '17 at 07:00
  • Shorter code and would generate less user-syntax errors produced by **incorrect xib names** if he reuses that kind of init for xib `UIViewController`s. Glad I could help – Zonily Jame Apr 17 '17 at 07:03
2
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
    }
Nischal Hada
  • 3,230
  • 3
  • 27
  • 57
  • This causes an exception because there is no main bundle as I've removed it from the project. Preferably I wouldn't need to keep this file since I won't be using it. – mbradber Apr 17 '17 at 06:35
1

change Your window root as and set 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
  }
Aamir
  • 16,329
  • 10
  • 59
  • 65
Lalit kumar
  • 1,797
  • 1
  • 8
  • 14
0

You just need to initialize viewcontroller object and implement white background color to its instance.

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow(frame: UIScreen.main.bounds)
    //Add below 2 lines
    let vc = DummyViewController()
    vc.view.backgroundColor = .white

    window?.rootViewController = vc
    window?.makeKeyAndVisible()
    return true
}
Manoj G.
  • 64
  • 1
  • 5