简体   繁体   English

iOS UINavigationController,pushViewController不起作用

[英]IOS UINavigationController, pushViewController not working

Good evening, 晚上好,

I currently have two UIViewControllers. 我目前有两个UIViewControllers。 My appDelegate looks like this 我的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;
}

The viewController looks like this: 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];
}

By clicking on the info button, that was added to the right side on the navigation bar, I want to switch to the secondView. 通过单击添加到导航栏右侧的信息按钮,我想切换到secondView。 This, however, is not happening because navigationController logs as nil ! 但是,这不会发生,因为navigationController记录为nil! What am I missing here? 我在这里想念什么?

Any help is truly appreciated! 任何帮助都由衷地感谢!

You don't have to create the window, it should already exist. 您不必创建窗口,它应该已经存在。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM