UINavigationcontroller中的问题

时间:2011-04-16 10:23:38

标签: iphone objective-c xcode

大家好  我正在尝试做一个简单的导航应用程序。这是我的代码

#import <UIKit/UIKit.h>
@class RootViewController;  
@interface jeeAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;
    UINavigationController *navigationController;
    RootViewController *viewcontroller;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;  
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;     
@property (nonatomic, retain) IBOutlet RootViewController *viewcontroller;

@end   

.m文件

#import "jeeAppDelegate.h"
#import "RootViewController.h"


@implementation jeeAppDelegate

@synthesize window;  

@synthesize navigationController;  




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

    navigationController=[[UINavigationController alloc]initWithRootViewController:viewcontroller];  
    [self.window addSubview:navigationController.view];  
    [self.window makeKeyAndVisible];  

    return YES;
}

对于此代码我提前感谢"Application tried to push a nil view controller on target <UINavigationController: 0x4b4f5c0>." ..

1 个答案:

答案 0 :(得分:3)

这很简单 - 在任何时候你都没有初始化你试图推入导航堆栈的视图控制器。

即:在你打电话之前..

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

...您需要确保viewcontroller实际存在,因为您需要allocinit

例如:

// Create our main menu view controller
MainMenuViewController *mainMenuVC = [[MainMenuViewController alloc] init];

// Create our navigational controller and init it with the main menu view controller
navController = [[UINavigationController alloc] initWithRootViewController:mainMenuVC];
[mainMenuVC release];

在上面mainMenuVC是我创建的自定义视图控制器。

此外,请注意,一旦您将视图控制器添加到导航控制器,您可以将其作为导航控制器发布并保留它。