我有3个视图控制器(VC)AB C.首先我提出A.然后我按B然后我按C.我按C后,我从堆栈中删除B,所以如果他按下,用户将返回A返回键。我使用此代码来推送C并删除B:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Nazaj" style:UIBarButtonItemStylePlain target:nil action:nil];
//we push C
PorabaControllerR *anotherViewController = [[PorabaControllerR alloc] initWithNibName:@"PorabaViewR" bundle:nil];
//[anotherViewController setTitle:@"Pregled porabe"];
[anotherViewController.navigationItem setBackBarButtonItem:backButton];
[self.navigationController pushViewController:anotherViewController animated:YES];
[anotherViewController release];
//we remove B from the stack
NSMutableArray *allControllers = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
[allControllers removeObjectAtIndex:[allControllers count] - 2];
//[allControllers objectAtIndex:[allControllers count] - 2]
[self.navigationController setViewControllers:allControllers animated:NO];
[allControllers release];
问题在于uinav。 item的后退按钮不会显示在C上,直到我上面并单击它。 (在B上没问题)。 有没有什么好方法可以在执行期间调试它或者回看按钮标题更改? 还有其他想法吗?
编辑:我尝试使用Vijay的想法:UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Nazaj" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonClick:)];
-(IBAction)leftBarButtonClick:(UIButton *) sender {
NSLog(@"clicked left");
//back to home screen
[self.navigationController popToRootViewControllerAnimated:YES];
}
但根本没有调用此函数,后退按钮仍然隐藏,直到我查看并单击它。
答案 0 :(得分:0)
只需在ur backbarbutton或leftbarbutton操作中使用此代码即可。
在你的内部功能
[self.navigationController popToRootViewControllerAnimated:YES];
OR
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
答案 1 :(得分:0)
我用这段代码解决了它:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Nazaj" style:UIBarButtonItemStylePlain target:nil action:nil];
PorabaControllerR *anotherViewController = [[PorabaControllerR alloc] initWithNibName:@"PorabaViewR" bundle:nil];
[anotherViewController.navigationItem setBackBarButtonItem:backButton];
NSMutableArray *allControllers = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
[allControllers removeObjectAtIndex:1]; //we remove B
[allControllers insertObject:anotherViewController atIndex:1]; //we push C
[self.navigationController setViewControllers:allControllers animated:YES];
[allControllers release];