关于“ios Application tried to push a nil view controller on target”这个错误,原因其实很简单,问题都是找到了才觉得简单,处理的时候不知道有多烦恼。呵呵,本来就应该是这样吧。

没有错误的作法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

rootViewController = [[WalkerUITestViewController alloc] initWithNibName:@"WalkerUITestViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];


self.window.rootViewController = navigationController;



self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

有错误的作法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

navigationController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];


self.window.rootViewController = navigationController;



self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

当然在这两个例子里面,属性的声明是一样的

1
2
@synthesize navigationController;
@synthesize rootViewController;

其实就是在使用之前,自己有木有初始化的问题。ok。