我试图自定义 UINavigationController
的后退按钮 . 在RootViewController中,我在 viewDidLoad
中设置了self.title,此字符串出现在导航栏中 . 在 -didSelectRowAtIndexPath
中,我创建子视图控制器,配置后退按钮并调用 -pushViewController
. 处理子进程将子视图控制器推入堆栈;我需要后退按钮弹出到初始视图,就像从第一个子视图控制器返回时一样 . Currenty backbutton将弹出到前一个视图,因此如果堆栈上有5个子视图控制器,我必须按回5次按钮才能进入根视图 . 显示后退按钮时,我无法启动操作 . 我在孩子VC时能够 popToRootViewController
;但是,后退按钮现在出现在根视图(!)上,我必须再次点击后退按钮以恢复原始 Headers 并删除后退按钮 . 这是root -viewDidLoad
的一部分:
- (void)viewDidLoad {
self.title = @"My Nav Bar Title"; // displays on root navigation bar title
// some setup code...
[super viewDidLoad];
}
这是 -didSelectRowAtIndexPath
的一部分,其中选择tableview单元格会导致子视图被压入堆栈:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ChildVC *child = [[ChildVC alloc]
initWithNibName:@"Child"
bundle:nil];
[self.navigationController dismissModalViewControllerAnimated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Quiz" style:UIBarButtonItemStylePlain target:self action:@selector(backToMenu)];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
[self.navigationController pushViewController:child
animated:YES];
[child release];
}
这是按下后退按钮时不会触发的动作方法:
-(void)backToMenu {
NSLog(@" in root backToMenu");
[self.navigationController popViewControllerAnimated:YES];
}
ChildVC还将在其 -didSelectRowAtIndexPath
中创建一个新子项并推送新的子控制器,作为下一个子项'page':
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Child *newChild = [[Child alloc]
initWithNibName:@"Child"
bundle:nil];
[self.navigationController dismissModalViewControllerAnimated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
self.title = self.quizString; // child view correctly displays customized title
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:@"Quiz"
style:UIBarButtonItemStylePlain
target:self
action:@selector(backToMenu)];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
[self.navigationController pushViewController:newQuestion
animated:YES];
[newChild release];
}
在Child -viewWillDisappear
我设置了一个全局变量,因此我知道何时推送新子节点以及何时弹回root:
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:YES];
if (startOver) {
[self backToMenu];
}
}
Child -backToMenu:
-(void)backToMenu {
[self.navigationController popToRootViewControllerAnimated:YES];
}
这是在Child中按下后退按钮的顺序:
- 调用子
-viewWillDisappear
,调用-backToMenu - -backToMenu调用popToRootViewControllerAnimated:
- 再次调用子-viewWillDisappear
,调用-backToMenu - root-viewWillAppear
- 控制返回Child -backToMenu
根视图显示正确,但导航栏包含后退按钮和 Headers ,就像它仍然是子视图一样 . 按后退按钮可删除后退按钮并恢复原始 Headers .
我怎样才能做到这一点?理想情况下,我想在堆栈上只有1个子视图,但我无法弄清楚如何;然后后退按钮将返回到根视图 . 但是当我尝试这个时,我得到了 NSInvalidArgumentException
', reason: '不支持多次推送相同的视图控制器实例...'
此外,当按下后退按钮时,为什么不触发动作的任何明显的事情?任何帮助都非常感谢... thx
2 回答
把这段代码放在你想要的视图控制器
[initWithNibName method]
表格中 pop at root view controller嗯,你的backButton调用
popToRootViewController
,它调用viewWillDissapear
,如果startOver为true,则调用popToRootViewController
AGAIN?如果它是假的,会发生什么?它继续前面叫popToRootViewController
...isn 't that if there redundant, since both it' s分支先前继续
popToRoot
或再次拨打popToRoot
?为什么不首先测试startOver(在你的backToMenu中),然后
popToRootViewController
如果是真的?