导航栏后退按钮不起作用

时间:2018-07-07 13:01:37

标签: ios objective-c uinavigationcontroller navigation uinavigationbar

我正在尝试添加自定义后退按钮,以便可以在弹出控制器之前执行一些任务。但是,无论我多么努力尝试将此功能与后退按钮绑定都无法正常工作。我正在添加一些代码来了解

-(void)customizeNavigationBar
{
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = @"Video Call";

   UIBarButtonItem *backButton = [[UIBarButtonItem alloc] 
              initWithTitle:@" " style:UIBarButtonItemStylePlain 
              target:self action:@selector(didBackBtnTap)];
   [self.navigationItem setBackBarButtonItem:backButton];
}

这是根本不调用的函数

-(void)didBackBtnTap
{
    [self popToProfileScreen];
}

请提出任何解决方案,谢谢。

4 个答案:

答案 0 :(得分:1)

使用leftbarbutton项代替setBackBarButtonItem

self.navigationItem.leftBarButtonItem = backButton;

答案 1 :(得分:1)

当您使用setLeftBarButtonItemsetBackBarButtonItem时,它将在您要推送到的viewController上生效,但在当前的viewController上无效。因此,您需要直接设置当前的leftBarButtonItem

答案 2 :(得分:0)

另一个选择是在viewWillDisappear中编写您的自定义代码。

答案 3 :(得分:0)

使用此代码

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 15, 15);
[backButton setImage:[UIImage imageNamed:@"backButtonImage"] forState:UIControlStateNormal];
[backButton setTitle:@"Back" forState:UIControlStateNormal];
backButton.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
[backButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

在xcasstes backButtonImage

中添加类似于NavigationBar的后退按钮的图像(15 * 15,@ 1x @ 2x @ 3x)
- (void)backButtonPressed {}