How to properly create a root view controller?

2019-04-16 09:30发布

问题:

After upgrading to xCode 4.2 I am getting the following warning...

Applications are expected to have a root view controller at the end of application launch

After reading as much as I could find on line about the RootViewController I am not sure whether I have created my root view controller properly. I created it a long time ago when I was first learning to program in xCode.

One question I have is it ok to name the root view controller something other than RootViewController. Every example I see now has it named RootViewController. I also see it synthesized in the app delegate like this...

@synthesize rootViewController = _rootViewController;

I do not understand what this is doing. Why not just...

@synthesize rootViewController;

In any event I changed the name of my root view controller to RootViewController and followed the example I found at cupsofcocoa.com. But even after the changes I am still getting the "...expected to have a root controller..." warning.

If someone has the time to take a look and let me know what I am missing, I have listed the the significant portions of my initialization code below.

Thanks,

John

  //RootViewController.h
#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController  {   

}
@end

.

  //RootViewController.m
#import "RootViewController.h"
#import "JetLoggerAppDelegate.h"

@implementation RootViewController
@end

.

  //JetLoggerAppDelegate.h   my app delegate 
#import <UIKit/UIKit.h>
@class RootViewController;

@interface JetLoggerAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    RootViewController *rootViewController;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
@end

.

  //.m app delegate
#import "JetLoggerAppDelegate.h"
#import "RootViewController.h"   //I don't think I need this here

@implementation JetLoggerAppDelegate

@synthesize window;
@synthesize rootViewController = _rootViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if ([launchOptions count] == 0) {
        _rootViewController = [[RootViewController alloc] init];
        self.window.rootViewController = self.rootViewController;
        [window makeKeyAndVisible];        
        return YES;

    }else{
        [JLHelper showAlertWithTitle:@"" message:[NSString stringWithFormat:@"launchOptions: %@", launchOptions]];

    }

    return NO;

}

.

  //main.m
#import <UIKit/UIKit.h>

int main(int argc, char *argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, @"JetLoggerAppDelegate");
    [pool release];
    return retVal;
}

回答1:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if ([launchOptions count] == 0) {
        _rootViewController = [[RootViewController alloc] init];
        self.window.rootViewController = self.rootViewController;
        **[window addSubview:_rootViewController.view];**
        [window makeKeyAndVisible];
        return YES;

    }else{
        [JLHelper showAlertWithTitle:@"" message:[NSString stringWithFormat:@"launchOptions: %@", launchOptions]];
 return NO;
    }
return nil;
}

Put return NO inside else statement and on the end put return nil; Hope this help.



回答2:

Applications are expected to have a root view controller

Replace in AppDelegate

 [window addSubview:[someController view]];

to

 [self.window setRootViewController:someController];