Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

objective c - Application windows are expected to have a root view controller at the end of application launch warning

I have been stuck with this warning for several hours now. I've looked around SO for answers, attempted all the ones I found and couldn't find the solution. Here's the run-down of the code I have, which Xcode generated by default.

This is in my AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

I have this on main.m (according to this answer)

int main(int argc, char *argv[])
{
    @autoreleasepool {
        int retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
        return retVal;
    }
}

I also have all the connections in my MainWindow.xib connected correctly. So I'm at a loss right now. Anything that I could be missing? Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's odd to be setting your window's rootViewController in application:didFinishLaunchingWithOptions: if you have a MainWindow.xib. Usually a project follows one of three templates:

  • Some projects have a MainWindow.xib. The target's “Main Interface” is set to “MainWindow” in the target's Summary tab (or in its Info.plist). This xib's File's Owner is UIApplication. The xib contains an instance of AppDelegate, connected to the File's Owner's delegate outlet. The xib also contains a UIWindow, whose rootViewController outlet is connected to a UIViewController (or subclass, such as UINavigationController), which is also in the xib. By the time the application delegate receives the application:didFinishLaunchingWithOptions: message, the xib is entirely loaded, so the window and its root view controller are already set up.

  • Other projects don't have a MainWindow.xib. The target's “Main Interface” is empty. Instead, the UIApplicationMain function creates an instance of AppDelegate, sets it as the UIApplication's delegate, and sends it the application:didFinishLaunchingWithOptions: message. The app delegate handles that message by creating a UIWindow, creating a view controller (or several), and setting the window's rootViewController property. The default version looks like this:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    
  • Some projects have a MainStoryboard.storyboard. I'm not going to describe this in detail because it doesn't seem relevant to your problem.

The problem you're describing makes it sound like you're using half of the first template, and half of the second template. That won't work. You need to decide which approach you're taking, and go all-in.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...