2016-06-07 25 views
1

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

+1

的[当按下的UINavigationController的背面栏按钮执行动作]可能的复制(http://stackoverflow.com/questions/27713747/execute-action-when-back-bar-button被按下) –

回答

0

如果您设置tintColor中导航栏,添加自定义后退按钮图像无题是着色颜色将反映图像的颜色。请按照这个苹果文档链接。

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 ]; 
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 
} 
+0

我想要显示警报,然后对警报进行操作,然后弹出视图控制器。 –

+0

我不想要后面的标题,只有后面的箭头。 –

+0

然后添加具有清晰颜色的自定义按钮,并且不要设置标题。只需添加无边框清晰颜色的按钮即可。所以原来的导航栏按钮将可见,并点击自定义按钮上的检测 – Lion

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 
     } 
} 
+0

我不想要后标题,只有后面的箭头。 –

相关问题