我正在尝试添加一个自定义后退按钮,以便我可以在弹出控制器之前执行一些任务。但是无论我多么努力,此功能都与后退按钮绑定不起作用。我正在添加一些代码来理解
-(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];
}
请提出任何解决方案,提前感谢。
使用leftbarbutton
项而不是setBackBarButtonItem
self.navigationItem.leftBarButtonItem = backButton;
当你使用setLeftBarButtonItem
或setBackBarButtonItem
时,它将在你推送到的 viewController 上生效,但不会在当前 viewController 上生效。这就是为什么您需要直接设置当前leftBarButtonItem
另一种选择是在viewWillDisappear中编写自定义代码。
使用此代码
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];
添加一个图像(15 * 15,@1x @2x @3x(,就像导航栏的后退按钮
一样- (void)backButtonPressed {}