UINavigationBar: intercept back button and back swipe gesture

萝らか妹 提交于 2019-12-03 20:14:48

To intercept the back swipe gesture you can set self as the delegate of the gesture (<UIGestureRecognizerDelegate>) and then return YES or NO from gestureRecognizerShouldBegin based on unsaved changes:

// in viewDidLoad
self.navigationController.interactivePopGestureRecognizer.delegate = self;

// ...
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {

        if (self.dirty) {
            // ... alert
            return NO;
        } else
            return YES;
    } else 
        return YES;
}

In the alert you can ask to the user if she want to go back anyway and, in that case, pop the controller in alertView clickedButtonAtIndex:

Hope this is of some help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!