我正在使用具有不同Controller的UINavigationController
。我已从导航中覆盖了BackButton
。在某些情况下,如果点按了RootController
,我想跳转到BackButton
。
但在这种情况下,用户跳回,但整个视图为空。 这种行为很奇怪,我不知道为什么。有人可以帮帮我吗。
要转到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];
}
答案 0 :(得分:0)
万一其他人到了这里-我有一个类似的问题使我有些困惑-原来是因为我正在使用自定义转换,将root vc的内容移出了屏幕。弹出根目录而未指定用于反转翻译的过渡,这意味着根目录随屏幕内容一起返回-不会自动重置为默认状态。
例如-如果您使用这样的UINavigationControllerDelegate设置自定义过渡:
class NavigationControllerDelegate: NSObject, UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
if let _ = fromVC as? FirstVC, let _ = toVC as? SecondVC {
return SlideTransitionAnimator(direction: .left)
}
}
和您的过渡类对rootViewController进行了一些转换-例如,就我而言,将其移出屏幕:
class SlideTransitionAnimator: NSObject, UIViewControllerAnimatedTransitioning {
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromView = transitionContext.view(forKey: .from),
let toView = transitionContext.view(forKey: .to) else {return }
let containerView = transitionContext.containerView
containerView.addSubview(toView)
switch direction {
case .left:
toView.frame.origin.x = toView.frame.size.width
default:
toView.frame.origin.x = -toView.frame.size.width
}
UIView.animate(withDuration: 1,delay: 0, options: .curveEaseInOut,
animations: {
fromView.alpha = 0
switch self.direction {
case .left:
fromView.frame.origin.x -= fromView.frame.size.width
default:
fromView.frame.origin.x += fromView.frame.size.width
}
toView.alpha = 1.0
toView.frame.origin.x = 0
},
completion: { _ in
transitionContext.completeTransition(true)
})
}
}
如果在调用popToRootViewController时不反转动画或重置动画,则最终可能会看到空的根视图控制器。