iOS App Goes to Black Screen

20,922

Solution 1

I finally solved this. After weeks of battling it and trying everything from ripping ALL of my code out, it turns out that the view outlet for my view in the storyboard had become double-linked to both "View" and one of my text fields in that view. Like this:

enter image description here

I have since deleted the linkage to the text field, but show the correct linkage here:

enter image description here

Interestingly, I am unable to reproduce this accidental connection in the UI, so I am not sure how that even happened. Anyway, I hope this helps others that might come across this.

Solution 2

I was faced with the same issue in my device. I managed to fix it by setting the application's main interface via xcode: MyApplicationMain InterfaceLaunchScreen.storyboard (or Main.storyboard).

enter image description here

Cheers! :)

Solution 3

I'd say the problem is this line:

    dispatch_async(dispatch_get_main_queue(), {
        self.performSegueWithIdentifier("toWelcomeView", sender: self);
    });

You're trying to do a segue to another view while this view has not yet appeared (view will appear). That's very iffy, and that's why sometimes it is in fact failing. I'm a bit surprised that you don't report seeing an error message in the Console warning that you're trying to segue from a view controller whose view is not yet in the interface.

As an interim measure, you might move your call to self.RetrieveRegistration() into viewDidAppear: and see if that helps.

But in the long term, you're just going about this the wrong way. Your goal seems to be to launch into one view controller under some circumstances and into another under other circumstances. The place to put all that logic is up front in applicationDidFinishLaunching, so that your rootViewController itself is different depending on the situation.

For example, imagine the following very simple situation: our storyboard contains just two view controller, RegistrationViewController and RootViewController (with a storyboard identifier "root"), with a presentation segue from the first to the second. In RegistrationViewController we ask for registration info, such as a "username", which we place in the NSUserDefaults. Then we segue to the RootViewController. All well and good, but our goal hereafter is that the user should never see RegistrationViewController again. This is easily taken care of in the app delegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    if let rvc = self.window?.rootViewController {
        if NSUserDefaults.standardUserDefaults().objectForKey("username") as? String != nil {
            self.window!.rootViewController = rvc.storyboard!.instantiateViewControllerWithIdentifier("root")
        }
    }
    return true
}

Solution 4

Make sure your first View Controller (in my case Navigation Controller) is Initial Controller. Find more details here.

Also, make sure when connecting Initial and Second Controller with Segue you press "Root View Controller" Relationship as you can see on picture here.

I hope it helps!

Share:
20,922
Matt Ray
Author by

Matt Ray

CEO at Technist Consulting. Engineering Manager at OneCause. Husband and Father. Sorts things ascendingly.

Updated on July 23, 2022

Comments

  • Matt Ray
    Matt Ray almost 2 years

    I am using a Launch Screen storyboard as well as a Main storyboard in my Swift app. Sometimes the app will show the launch storyboard and then go to a black screen. Other times it correctly goes to the Main storyboard entry point. It almost seems like it is build-dependent in that if I clean and rebuild a few times, a build pop out that behaves properly. Any ideas what may be causing this?

    To add some clarification:

    • The SplashScreen.storyboard is set in the target settings (good question).
    • This happens both on real devices and in simulator BUT NOT ALWAYS.
    • I am not doing any networking (synchronous or otherwise) in this stage
    • The blackout seems to happen only when transitioning to one specific view
    • The code for the view's viewWillDisplay and such executes even when blacked out, just nothing VISIBLE is displayed

    This might help: I put in the following code in the offending view:

    override func viewDidAppear(animated: Bool) {
        print(self.view.layer.bounds.width);
        print(self.view.layer.bounds.height);
    
    }
    

    When the black screen issue occurs, I get 260 and 17. When the view presents correctly, I get the 5s (simulator) dimensions of 320 and 568. Does that help anyone help me?

    • Adrian
      Adrian about 8 years
      In Project Navigator, click on your project. Then click Target (not Project), and scroll down to App Icons and Launch Images. I suspect your issue lies with your Launch Screen File.
    • Teja Nandamuri
      Teja Nandamuri about 8 years
      is your root controller set properly ? Are you changing the root controller programmatically anywhere in ur code ?
    • matt
      matt about 8 years
      Show your code, please. Are you doing anything illegal like networking synchronously as the app launches?
    • Matt Ray
      Matt Ray about 8 years
      @AdrianB The launch screen file is set to the correct LaunchScreen.storyboard. The storyboard displays properly, it is just afterwards that it goes to black. And only sometimes.
    • Matt Ray
      Matt Ray about 8 years
      @TejaNandamuri I don't think so. I do have some code in viewWillAppear in ViewController.swift that is being used by my root view controller. In it, I am checking to see if the person has already registered for service. If they have, I send them on (via a dispatched segue) to the next view. If not, I do nothing and let the registration screen display (the root view).
    • Matt Ray
      Matt Ray about 8 years
      @matt There isn't really much relevant code to show. I will edit and paste in the only function being called in viewWillAppear.
    • Vatsal Shukla
      Vatsal Shukla over 4 years
      it can be another reason. please checkout my answer. stackoverflow.com/a/58217288/4131763
  • Matt Ray
    Matt Ray about 8 years
    Point taken. I will refactor and try moving this code upstream. That being said, I actually see NOTHING in the console (at all) when this happens. Strange, right? Also, the segue code you mentioned is not being executed in these instances since the conditions are not satisfied.
  • Matt Ray
    Matt Ray about 8 years
    Quick after-action report. Moved call to that function into viewDidAppear: with no change in presented behavior. It is almost like the re-build process sometimes correctly links the post-splash call and other times it just doesn't. Thank you for your ongoing thought on this. This has been bugging me for WEEKS.
  • matt
    matt about 8 years
    I assure you that this is not due to the rebuild process. It's about your code.
  • matt
    matt about 8 years
    I added code to show a proper typical app delegate-based strategy for presenting a registration screen once, which is what you seem to be trying to do.
  • matt
    matt about 8 years
    I've created a downloadable project at github that you can examine to see that it works: github.com/mattneub/RegistrationExample
  • Matt Ray
    Matt Ray about 8 years
    Appreciate that. Main difference I can see is that in my main storyboard I have an empty Navigation Controller as my root that immediately segues to another view. I am wondering if the issue is in this handoff in conjunction with my code creating some sort of race condition that fails only sometimes. I am going to try to merge your approach with using a Navigation Controller to see how that looks. Any thoughts on that?
  • Matt Ray
    Matt Ray about 8 years
    Just a follow-up: after a few clean & build runs, I received the black screen again. I have implemented the suggestion here by moving my root view logic to the app delegate. Any other tips?
  • Nikhil Manapure
    Nikhil Manapure over 6 years
    I believe this happens sometimes when we copy paste whole view... atleast happened to me.
  • Vatsal Shukla
    Vatsal Shukla over 4 years
    it can be another reason also. please checkout my answer stackoverflow.com/a/58217288/4131763
  • JCutting8
    JCutting8 over 2 years
    I had accidentally selected another view controller to be the initial, and realising my mistake unchecked it again (but from that moment on, no view controller was initial!). Thanks