Confirm back button on UINavigationController

前端 未结 7 1847
南旧
南旧 2020-12-09 18:03

I am using the storyboard for my app which is using UINavigationController. I would like to add a confirmation dialog to the default back button so

7条回答
  •  时光取名叫无心
    2020-12-09 18:54

    Actually, you can find the Back button view and add UITapGestureRecognizer to it.

    If you look at this image: Back button screen Using this code:

    @interface UIView (debug)
    - (NSString *)recursiveDescription;
    @end
    
    @implementation newViewController
    ... 
    NSLog(@"%@", [self.navigationController.navigationBar recursiveDescription]);
    

    You can realize how to find the View of the Back button. It is always the last one in subviews array of the navigationbar.

    2012-05-11 14:56:32.572 backBtn[65281:f803] >
       | >
       | >
       |    | >
       |    | >
       | >
       | >
    

    So I used:

    UIView *backButton = [[navBar subviews] lastObject];
    [backButton setUserInteractionEnabled:YES];
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alertMsg)];
    [tapGestureRecognizer setNumberOfTapsRequired:1];
    [backButton addGestureRecognizer:tapGestureRecognizer];
    

    Tapping on back button and voilà: Back button was tapped

提交回复
热议问题