IOS UINavigationController,pushViewController不工作

IOS UINavigationController,pushViewController不工作,ios,uiviewcontroller,uinavigationcontroller,pushviewcontroller,Ios,Uiviewcontroller,Uinavigationcontroller,Pushviewcontroller,晚上好, 我目前有两个UIViewController。我的appDelegate看起来像这样 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { struct CGRect rect = [[UIScreen mainScreen] bounds]; rect.origin.x = rect.origin.y

晚上好,

我目前有两个UIViewController。我的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];
}
单击导航栏右侧添加的info按钮,我想切换到secondView。然而,这并没有发生,因为navigationController记录为nil!我错过了什么


非常感谢您的帮助

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

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

谢谢你的提示!我更改了代码,但它不起作用!它说,在应用程序启动结束时,您需要在windowself.window.rootViewController=nc上设置rootViewController;铁路,thx!!成功了!但是我必须添加self.window alloc/initWithFrame才能使它工作!使用self.window和_window的区别到底是什么?