答案 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,请添加不带标题的自定义后退按钮图像,色调颜色将反映图像颜色。请关注此苹果文档链接。
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 ];