【问题标题】:How to get UIBarButtonItem to call "pushViewControllerAnimated:(BOOL)" to the UINavigationController?如何让 UIBarButtonItem 向 UINavigationController 调用“pushViewControllerAnimated:(BOOL)”?
【发布时间】:2011-08-26 14:32:34
【问题描述】:

我正在尝试使用 UINavigationController 进行一种“从左到右”的分层导航。我正在使用此代码:

-(IBAction)showSettings:(id)sender {
UINavigationController *aNavController = [[UINavigationController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:aNavController animated:YES];

SecondView *secondView = [[SecondView alloc] initWithNibName:nil bundle:nil];
[aNavController pushViewController:secondView animated:NO];

UIBarButtonItem *aButton = [[UIBarButtonItem alloc] initWithTitle:@"Knapp" style:UIBarButtonItemStylePlain target:self action:@selector(nextView:)];
aNavController.topViewController.navigationItem.rightBarButtonItem = aButton;

[aButton release];
[secondView release];
[aNavController release]; }

-(IBAction)nextView:(id)sender {
ThirdView *thirdView = [[ThirdView alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:thirdView animated:YES];
NSLog(@"Push it like it's hot"); }

'nextView' 被正确调用,“Push it like it's hot”打印在“Output”中,但没有推送任何内容,也没有显示新视图。我也尝试过改变: [self.navigationController pushViewController:thirdView animated:YES];[self.navigationController popViewControllerAnimated:YES]; 但结果保持不变,没有任何反应。

我的结论是:我在“nextView”的 self.navigationController 部分做错了,即程序不知道从/到什么推/弹出。我已尽力查看有关此的某种文档,但我无法解决,所以我在这里。

self.navigationController 是正确的方法还是我在该行中遗漏了什么?

提前谢谢你, 托拜厄斯·托维达尔

【问题讨论】:

    标签: iphone uinavigationcontroller uibarbuttonitem pushviewcontroller popviewcontroller


    【解决方案1】:

    当您使用self.navigationController 时,您不会访问在showSettings 中定义为aNavController 的导航控制器。 self.navigationController 指的是本端视图控制器可能被推送的导航。我假设定义此代码的视图没有 NavigationController。这意味着self.navigationController 指向空值。 您可以在 nextView 方法中通过 NSLog(@"%@", self.navigationController); 验证这一点

    您需要做的是在您的头文件中将aNavController 设为实例变量,然后您可以在nextView: 方法中执行[aNavController pushViewController:thirdView animated:YES];

    【讨论】:

    • 万岁!你是绝对正确的,NSLog(@"%@", self.navigationController);确实输出(null)。在标题中添加aNavController作为变量解决了它,感谢您的帮助! /托比亚斯
    【解决方案2】:

    你的问题似乎是

    UINavigationController *aNavController = [[UINavigationController alloc] initWithNibName:nil bundle:nil];
    [self presentModalViewController:aNavController animated:YES];
    

    将一个新的模型视图控制器推入堆栈,并且不使这个导航控制器成为当前视图控制器的导航控制器;

    所以你需要将当前视图控制器的导航控制器设置为aNavController 或将aNavController 设为类变量并使用它来推送thirdView。

    【讨论】:

    • 是的,这也是正确的,谢谢你的回答迈克尔!
    猜你喜欢
    • 1970-01-01
    • 2011-08-15
    • 2012-01-15
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多