0

おはよう、IOS UINavigationController、pushViewController not working

私は現在2つのUIViewControllerを持っています。ナビゲーションバーの右側に追加された情報ボタン、をクリックすると

- (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]; 
} 

、私はに切り替えたい:私appDelegateはViewControllerを、このようになります。この

- (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; 
} 

のように見えますsecondView。しかし、これは、navigationControllerがnilとしてログを記録するので起こりません!私はここで何が欠けていますか?

本当にありがとうございます。

答えて

1

ウィンドウを作成する必要はありません。すでに存在しているはずです。

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

ヒントありがとうございます!私はコードを変更しましたが、機能しません!それは、アプリケーションの起動の最後にrootViewCintollerが期待していると言う。 – Pascal

+0

あなたはウィンドウにrootViewControllerを設定する必要があります。 – railwayparade

+0

self.window.rootViewController = nc; – railwayparade