在没有UINavigationBar iOS标题的后退按钮上添加警报

时间:2016-06-07 11:19:41

标签: ios

我有一个带导航栏的屏幕,默认的后退按钮是使用navigationItem storyboard放置的。我想根据条件在后退按钮上弹出视图控制器。添加事件之前,后退按钮是默认的。请为此问题提出解决方案。enter image description here

3 个答案:

答案 0 :(得分:2)

你应该实现这个方法,

 -(void)willMoveToParentViewController:(UIViewController *)parent{

     if (parent == nil) {

      // handle your back button's task here
     }
  }

在导航到parentview控制器之前调用此方法。

如果它在您的情况下不起作用,那么您可以使用自定义后退按钮,例如

  - (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack:)];
    self.navigationItem.leftBarButtonItem=newBackButton;
}

-(void)goBack:(UIBarButtonItem *)sender {

        //Handle your back button's task here and you have to call popViewcontroller your self in this case
}

答案 1 :(得分:0)

UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack:)];
self.navigationItem.leftBarButtonItem=newBackButton;

-(void)goBack:(UIBarButtonItem *)sender {

       UIAlertView *removeAddAlertView = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@“Are you want to go Back ” delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Cancel", nil];
       [removeAddAlertView show];

}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
       if (buttonIndex == 0) {
           // cancel
        }else if (buttonIndex == 1) {
          // ok
        }
}

答案 2 :(得分:0)

如果设置tintColor Of NavigationBar,请添加不带标题的自定义后退按钮图像,色调颜色将反映图像颜色。请关注此苹果文档链接。

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/index.html#//apple_ref/doc/uid/TP40012857-UIView-SW7

UINavigationItem *navItem = [[UINavigationItem alloc] init]; navBar.tintColor = self.tintColor;

UIImage *myImage = [UIImage imageNamed:@"left_arrow.png"]; myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStylePlain target:self action:@selector(cancelButtonFunction:)];
 navItem.leftBarButtonItem = leftButton;
navBar.items = @[ navItem ];