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

相關問題