2

Recently I've got stuck in a bug of pushing UIViewController to UINavigationViewController, and I've found the solution for it.

Bug scenario:

it doesn't matter how you push the UIViewController (by segue, using pushViewController, ..). In RootViewController of your NavigationViewController, try to use swipe back gesture! (nothing should happen), then try to push a ViewController into your NavigationViewController by tapping an element. => App Freezes! Here, if you capture the screen by XCode, you will find that the current visible screen is the next screen!! (but it's not!), Cpu usage and any other things are normal. you can get out of App Freezing by just another swipe back gesture!

Here is the solution:

In your BaseNavigationViewController (which extends UINavigationViewController) insert these codes (especially when you're using UITabbarNavigationViewController)

- (void)navigationController:(UINavigationController *)navigationController
   didShowViewController:(UIViewController *)viewController
                animated:(BOOL)animate {
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
      if (self.viewControllers.count > 1) {
        self.interactivePopGestureRecognizer.delegate = self;
        self.interactivePopGestureRecognizer.enabled = YES;
      } else {
        self.interactivePopGestureRecognizer.delegate = nil;
        self.interactivePopGestureRecognizer.enabled = NO;
      }
    }
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
   if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
      return self.viewControllers.count > 1;
   }
   return NO;
}

Do not forget to write these codes too:

in viewDidLoad of BaseNavigationViewController:

self.delegate = self;

and your BaseNavigationViewController must use these two protocols: UINavigationControllerDelegate, UIGestureRecognizerDelegate

Sport
  • 8,570
  • 6
  • 46
  • 65
Aliunco
  • 399
  • 2
  • 9
  • Is this a question or an answer? – Au Ris May 30 '18 at 07:24
  • 2
    @AuRis maybe OP faced trouble with this problem and figured he would try and help others who had it. – Alan May 30 '18 at 11:36
  • @OP For you to face this issue you probably have set up something incorrectly, it is not normal behavior for an app to freeze just when you push a view controller, it is also not common to set the delegate of something to itself. Delegates are used so that a class can respond to actions done in another class completely. It should already be able to detect if things have happened in it's own class. – Alan May 30 '18 at 11:38
  • 1
    @AuRis Actually it's problem that I faced recently, and I just wanted to help the others like me. – Aliunco May 30 '18 at 11:47
  • 1
    @AlanS you're right is not a normal behavior of pushViewController, unfortunately, it seems like a bug for iOS SDK, and it will never happen in normal cases, read the Bug scenario. – Aliunco May 30 '18 at 11:49
  • @Aliunco This situation does not happen for me. If i swipe back multiple times on the root nothing happens, as you said, but the device does not get stuck. I am certain that this is not a bug with the iOS SDK and that you've done something incorrectly. The issue in the first place should not happen naturally, without you having interfered in some way. – Alan May 30 '18 at 12:07
  • @AlanS I've searched on my code and tried to find anything weird, which can affect this behavior and I've found something! you're right there was something wrong with the code! you can find the bug here : https://github.com/aliuncoBamilo/TestNavigationPushBug – Aliunco May 30 '18 at 13:58
  • 1
    If you wish to post a problem and a solution that is fine but please do it correctly. Post the issue in your question. Then post the solution as an answer below. Do not post both together in the question. Please remove the solution from your question and post it as an answer below. – rmaddy May 30 '18 at 14:00
  • This is the same issue as described here: https://stackoverflow.com/questions/36503224/ios-app-freezes-on-pushviewcontroller/36637556 – lenooh Oct 23 '18 at 18:10
  • Possible duplicate of [iOS App Freezes on PushViewController](https://stackoverflow.com/questions/36503224/ios-app-freezes-on-pushviewcontroller) – lenooh Oct 23 '18 at 18:11

0 Answers0