Push View Controller, Black Screen

18,294

Solution 1

When you're using a storyboard, and you want to push a view controller in code (rather than with a segue), you need to give the controller an identifier, and create it like this:

    ImagesViewController *ivc = [self.storyboard instantiateViewControllerWithIdentifier:@"MyIdentifier"];
    ivc.label = self.label.text;
    [self.navigationController pushViewController:ivc animated:YES];

Solution 2

Xcode 7.3 and Swift 2.2

In my case, I had made changes in the storyboard and made it a TabBarController and accordingly changed the class of the controller from UIViewController to UITabBarController. After some tweaking, this change wasn't favourable and I un did all the changes and got a black screen then because I had forgotten to change the class of the controller. I changed it back to UIViewController and it started working again.

So check if you have made the same mistake. The black screen came because the storyboard had a class(UIView/UITabBar/UITableView Controller) but that wasnt the same in code.

Solution 3

The view controller you are pushing is not having any frame dimension set.It is always recommended to call designated init for objects. For view controllers, designated init method is

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle

if you have a xib assign it like

ImagesViewController *ivc = [[ImagesViewController alloc] initWithNibName:<your xib> bundle:[NSBundle mainbundle];

if you are using custom view, assign a frame dimension and add it as subview

Solution 4

This can also happen if you have somehow got an incorrect connection between one of the subviews in the storyboard to the controller's view. Check the Referencing Outlets are correct in each of your subviews.

Solution 5

I got a good one:

Make sure you are implementing the right Super class, delegate, etc.. in the top part of the viewController you are trying to present. i.e.

I wasn't using/implementing UINavigationController at all

class TMDetailBlogViewController: UINavigationController {
  //code goes here
}

After

class TMDetailBlogViewController: UIViewController {
  //code goes here
}
Share:
18,294

Related videos on Youtube

Brandon Houlihan
Author by

Brandon Houlihan

Updated on September 15, 2022

Comments

  • Brandon Houlihan
    Brandon Houlihan over 1 year

    I'm pushing to a new view controller and passing some data to it. When I run the application I can press the button and push to a new view but the screen is completely black. Any help is appreciated.

    - (IBAction)button:(id)sender {
    
        NSString *firstField = self.field.text;
        NSString *secondField = self.field2.text;
    
        self.resultsArray = [[NSArray alloc] initWithObjects:firstField, secondField, nil];
    
        NSUInteger randomResult = arc4random_uniform(self.resultsArray.count);
        self.label.text = [self.resultsArray objectAtIndex:randomResult];
    
        ImagesViewController *ivc = [[ImagesViewController alloc] init];
        ivc.label = self.label.text;
        [self.navigationController pushViewController:ivc animated:YES];
    
    }
    
    • A'sa Dickens
      A'sa Dickens almost 11 years
      what is the context of this ? are you allocating the view controller from storyboard? a xib file? no thing at all just randomly?
  • Brandon Houlihan
    Brandon Houlihan almost 11 years
    I don't have a xib. I'm creating the UI elements from the storyboard.
  • Paul Dardeau
    Paul Dardeau almost 11 years
    In that case, you probably want to make use of this method to handle your view transition: - (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender
  • Brian White
    Brian White over 10 years
    The storyboard object can be retrieved with [UIStoryboard storyboardWithName:@"Main" bundle: nil]; where "Main" is the storyboard's filename without the extension (so could also be "Main_iPhone" or "Main_iPad").
  • rdelmar
    rdelmar over 10 years
    @BrianWhite, true, but if the calling controller is in the same storyboard as the new controller, it's shorter to just use self.storyboard.
  • Brian White
    Brian White over 10 years
    Sure. I was trying to access it from the AppDelegate which did not have that property and so had to do some more searching. I figured I'd append what I learned so others might avoid the extra work if they were trying to do the same.
  • Rodrigo Venancio
    Rodrigo Venancio about 10 years
    Anyone that tried this and had the "Expected ']'" warning, put a ':'before "@MyIdentifier", so it will stay like this: instantiateViewControllerWithIdentifier:@"MyIdentifier"
  • rdelmar
    rdelmar about 10 years
    @RodrigoVenancio, thanks for catching that typo. I've corrected the post.
  • Rodrigo Venancio
    Rodrigo Venancio about 10 years
    @rdelmar And thank you for your answer, it solved my problem! I tried to edit your answer but stackoverflow didn't allow me.