Brandon Houlihan
Brandon Houlihan

Reputation: 209

Push View Controller, Black Screen

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];

}

Upvotes: 20

Views: 18847

Answers (7)

Sneha Priya Ale
Sneha Priya Ale

Reputation: 21

I was trying without using storyboard, and its just that the default screen it uses is in black color. I changed the background color to white and it worked.

Pushed the controller this way-

NextController *nextController = [[NextController alloc]init]; [self.navigationController pushViewController:nextController animated:YES];

In NextController-

(void)viewDidLoad{
[self.view setBackgroundColor:[UIColor whiteColor]];

}

Upvotes: 0

Idelfonso Gutierrez
Idelfonso Gutierrez

Reputation: 330

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
}

Upvotes: 1

Yash Tamakuwala
Yash Tamakuwala

Reputation: 2056

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.

Upvotes: 2

sdsykes
sdsykes

Reputation: 1256

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.

Upvotes: 1

rdelmar
rdelmar

Reputation: 104082

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];

Upvotes: 51

slysid
slysid

Reputation: 5498

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

Upvotes: 2

Paul Dardeau
Paul Dardeau

Reputation: 2619

Typically, you transition to another view controller by calling:

initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

on your custom UIViewController.

If you're not using a xib file, then what you're doing may be fine. Are you dynamically creating your UI elements within the constructor of your ImagesViewController?

Upvotes: 0

Related Questions