Why am I seeing a black screen? Did I not alloc so

2019-08-17 15:20发布

问题:

In xcode, this is one of the views I have created:

If I run the program in the simulator, I can see this view fine. However, if I create a UIViewController class and hook it up with this view (I confirmed this view is a UIViewController), this is what I get in the simulator:

Why do I get this blank screen? How can I get the screen that looks like the first image?

Edit: code

#import "EnterLevelViewController.h"

@interface EnterLevelViewController ()

@end

@implementation EnterLevelViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)loadView
{
    // Implement loadView to create a view hierarchy programmatically, without using a nib.
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

回答1:

It appears from your code that you have uncommented the -(void)loadView method from the view controller, which is provided in the template. When you do this, the controller will try to construct the view programmatically, instead of using the nib (storyboard). Remove the empty method completely and see if that helps.



回答2:

just drag from the tabBarController to the new ViewController in the storyboard and when you let go, press the selection for relationship ViewController.

Here is a quick example of how to set it up with three tabs, one with your view:

https://github.com/HubertK/TabBar_Example



回答3:

The storyboard that was part of your project at the outset already included a view controller and its associated view. You can see it in the storyboard image that you posted -- it's the icon at the bottom with the light gray background. That view controller was set up as the root view controller, and the code that came as part of the project template loaded that view controller and its view. That's what you were seeing in the simulator. When you added another view controller, you must have configured it as the root controller instead. Since it doesn't have a view connected to it (or if it does, the view is empty), you no longer see anything.