【发布时间】:2014-10-30 11:08:28
【问题描述】:
我正在使用带有不同控制器的UINavigationController。我已经覆盖了导航中的BackButton。在某些情况下,如果点击了BackButton,我想跳转到RootController。
但在这种情况下,用户会跳回,但整个 View 是空的。 这种行为很奇怪,我不知道为什么。谁能帮帮我。
要转到RootController,我执行以下操作。
if([self.tempnavigationInfo.refkey isEqual:@"main"]){
[self.navigationController popToRootViewControllerAnimated:NO];
}
对于BackButton,我已经实现了这个处理程序:
@implementation UINavigationController (ShouldPopOnBackButton)
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
if([self.viewControllers count] < [navigationBar.items count]) {
return YES;
}
BOOL shouldPop = YES;
UIViewController* vc = [self topViewController];
if([vc respondsToSelector:@selector(navigationShouldPopOnBackButton)]) {
shouldPop = [vc navigationShouldPopOnBackButton];
}
if(shouldPop) {
dispatch_async(dispatch_get_main_queue(), ^{
[self popViewControllerAnimated:YES];
});
} else {
// Workaround for iOS7.1
for(UIView *subview in [navigationBar subviews]) {
if(subview.alpha < 1.) {
[UIView animateWithDuration:.25 animations:^{
subview.alpha = 1.;
}];
}
}
}
return NO;
}
在 ViewController 中我是这样处理的:
-(BOOL) navigationShouldPopOnBackButton {
if (self.backConfirmation != nil
&& self.backConfirmation.navigation != nil
&& self.backConfirmation.confirmation == nil){
[[NSNotificationCenter defaultCenter] postNotificationName:@"gotoWithoutConfirmation" object:self.backConfirmation.navigation];
return NO;
}
return YES;
}
在我的 MainController 中,我捕获通知并使用上面的“popToRoot”逻辑调用该方法:
-(void) handleNotificationFromFormView:(NSNotification*) notification
{
if([notification.name isEqualToString:@"gotoWithoutConfirmation"]){
self.tempnavigationInfo = (NavigationInfo*)notification.object;
[self goBackTo];
}
【问题讨论】:
-
请添加更多代码
-
请告诉我们您是如何返回到其他视图控制器的。您是在从视图中消失的同时删除所有子视图还是其他。
-
我在帖子中添加了后退按钮逻辑。