0

晚上好,IOS UINavigationController,pushViewController不工作

我目前有两个UIViewControllers。我的appDelegate看起来像这样

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    struct CGRect rect = [[UIScreen mainScreen] bounds]; 
    rect.origin.x = rect.origin.y = 0.0f; 

    _viewController = [[sandboxViewController alloc] init]; 

    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:_viewController]; 

    _window = [[UIWindow alloc] initWithFrame:rect]; 

    [_window makeKeyAndVisible]; 
    [_window addSubview:nc.view]; 

    return YES; 
} 

的的viewController看起来是这样的:

- (void)loadView { 
     self.view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)]; 
     self.view.backgroundColor = [UIColor whiteColor]; 
     self.navigationItem.title = @"Master View"; 
    } 

    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
     [infoButton addTarget:self action:@selector(switchView:) forControlEvents:UIControlEventTouchUpInside]; 
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton]; 
    } 

    - (void)switchView:(id)obj { 
     if(![self navigationController]) 
      NSLog(@"navigationController IS NIL!!!"); 
     if(!_secondView) 
      _secondView = [[secondViewController alloc] init]; 
     [self.navigationController pushViewController:_secondView animated:YES]; 
} 

通过点击信息按钮,已添加到右侧的导航栏上,我想切换到secondView。然而,这是不会发生,因为navigationController日志,零!我在这里错过了什么?

任何帮助是真正的赞赏!

回答

1

您不必创建窗口,它应该已经存在。

//_window = [[UIWindow alloc] initWithFrame:rect]; //remove this line  
[self.window makeKeyAndVisible]; //use the ivar 
[self.window addSubview:nc.view]; 
+0

谢谢你的提示!我改变了代码,但它不起作用!它说,在应用程序启动预计年底rootViewCintoller – Pascal 2011-12-15 06:30:28

相关问题