我被困在试图定制UINavigationController的后退按钮。 在RootViewController中,我在viewDidLoad中设置了self.title,并且此字符串出现在导航栏中。在-didSelectRowAtIndexPath我创建子视图控制器,配置后退按钮并调用-pushViewController。对子进行处理会将子视图控制器推入堆栈;我需要后退按钮弹出到初始视图,就像从第一个子视图控制器返回时一样。当前的后退将弹出到先前的视图,所以如果堆栈中有5个子视图控制器,我必须按回5次按钮才能进入根视图。 当后退按钮显示时,我无法取消操作。在子VC中,我能够popToRootViewController;然而,现在回滚按钮出现在根视图(!)上,我必须再次按下回退按钮才能恢复原始标题并移除后退按钮。 这里是根-viewDidLoad的一部分:UINavigationController popToRootViewController不重置标题,清除返回按钮
- (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];
}
这里是没有按操作方法“T火的时候,按下后退按钮:
-(void)backToMenu {
NSLog(@" in root backToMenu");
[self.navigationController popViewControllerAnimated:YES];
}
ChildVC也将创造在其-didSelectRowAtIndexPath一个新的子和推新Child C级ontroller,作为下一子页面“:
- (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];
}
儿童-viewWillDisappear我设置一个全局变量,所以我知道什么时候,当弹出回根推新子和:
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:YES];
if (startOver) {
[self backToMenu];
}
}
Child -backToMenu:
-(void)backToMenu {
[self.navigationController popToRootViewControllerAnimated:YES];
}
这里当返回按钮被按下子序列:
- 儿童-viewWillDisappear被调用,调用-backToMenu - -backToMenu调用popToRootViewControllerAnimated: - 儿童-viewWillDisappear再次调用,调用-backToM ENU - 根-viewWillAppear调用 - 控制返回到儿童-backToMenu
根视图显示正确,但导航栏包含后退按钮和标题一样,它仍然是一个子视图。按下后退按钮可删除后退按钮,恢复原始标题。
我该如何做这项工作?理想情况下,我想只在堆栈中有1个子视图,但我无法弄清楚如何;那么后退按钮将返回到根视图。但是当我尝试这样做时,我得到NSInvalidArgumentException',原因:'不止一次推送相同的视图控制器实例...'
另外,任何显而易见的原因为什么按钮时按钮不会触发操作?任何帮助非常感谢... thx