导航控制器无法在后退按钮上弹出推送视图

3

在设置了简单的导航控制器(启动基于导航的应用程序项目)后,我创建了一个带有XIB文件的新视图。

在我的HomeViewController(主屏幕上所有选项都是UIButton)中,我有:

@implementation HomeViewController

-(IBAction) optionChoosed:(UIButton *)button
{
    NSString *msg = [NSString stringWithFormat:@"Button: %d", button.tag];
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Hi" message:msg delegate:nil cancelButtonTitle:@"Go away" otherButtonTitles:nil];
    
    switch (button.tag) {
        case 13:
            // Simple Search
            [self loadSimpleSearch]; break;
            
        default:
            [alert show];           
            break;
    }
    [alert release];
}

-(void)loadSimpleSearch
{
    SimpleSearchViewController *controller = 
        [[SimpleSearchViewController alloc] initWithNibName:@"SimpleSearchViewController" bundle:nil];
    
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}

这很棒!它将视图推到堆栈的前面!

现在,因为在我的第二个视图SimpleSearchViewController中,我有self.title = @"myTitle";,所以我在导航栏中得到了标题和返回按钮(因为我在HomeViewController上也有相同的设置)。

我认为NavigationViewController会处理当前视图的弹出,但它没有。

我该怎么做才能弹出SimpleSearchViewController

我在哪里使用[self.navigationController popViewControllerAnimated:YES];

因为视图还在那里,而ViewDidUnload从未被调用。

我的想法是这应该在第一个视图控制器HomeViewController中处理,但我不知道我应该连接到哪个方法,我阅读了文档,但我无法搞清楚:-/

非常感谢任何帮助。

HomeViewController

alt text http://cl.ly/XNS/Screen_shot_2010-04-21_at_22.38.51.png

SimpleSearchViewController

alt text http://cl.ly/YDw/Screen_shot_2010-04-21_at_22.40.00.png

SimpleSearchViewController按下返回按钮后

alt text http://cl.ly/XLO/Screen_shot_2010-04-21_at_22.40.21.png


添加一个来自评论的图片,询问HomeViewController是否为NavigationViewController的根控制器

alt text


你能添加SimpleSearchViewController的代码,设置标题吗? 你是自己放置返回按钮吗? - Laurent Etiemble
不,我不是。这是自动完成的。你只需要在ViewDidLoad事件中设置标题 self.title = @"Home"; 即可。 - balexandre
你是否将HomeViewController设置为NavigationViewController的根控制器? - Laurent Etiemble
是的,正如您可以从图像http://cl.ly/ZFa中看到的那样,它是根目录,或者我错了 :-( - balexandre
是的,这有点奇怪。事实上,我和你一样遇到了同样的问题,对于某些子控制器,它可以正常工作(dealloc被调用),而对于其他控制器则不行。但是今天我学到了一个关键问题:viewWillDisappear是一个很好的帮手。 :-) - Sebastian Roth
显示剩余2条评论
1个回答

1

不确定这是否有帮助,但在代码中实现navigationItem时,如果您不调用super,则弹出功能将不存在。

-(UINavigationItem*) navigationItem
{
    UINavigationItem* item = [super navigationItem];
    item.title = @"search";
    return item;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接