未调用 UIBarbuttonItem 的操作 ios

action of UIBarbuttonItem not called ios

我正在尝试调用 UIBarButtonItem 当控制器的后退按钮是 pressed.This 是我的 code.I 已将初始化放在 viewDidLoadviewWillAppear 但未调用该方法。

UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:self action:(@selector(backToViewController:))];

self.navigationItem.backBarButtonItem = exampleButton;

-(void)backToViewController:(UIBarButtonItem*)sender
 {
     [self.navigationController popToRootViewControllerAnimated:YES];
 }

IT 不会将 backBarButtonItem 添加为您的行:

self.navigationItem.backBarButtonItem = exampleButton;

您必须提供 leftbarbutton 作为

UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithTitle:@"  " style:UIBarButtonItemStylePlain target:self action:(@selector(backToViewController:))];
// As you are going with  "  ", there is no text, that means you have to click at left side randomly, to get affect.
self.navigationItem.leftBarButtonItem = exampleButton;

更新 1

您可以根据需要为自定义按钮提供您创建的图像,

UIButton *barCutomBtn =  [UIButton buttonWithType:UIButtonTypeCustom];
[barCutomBtn setImage:[UIImage imageNamed:@"backImage.png"] forState:UIControlStateNormal];
[barCutomBtn addTarget:self action:@selector(backToViewController:)forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithCustomView:barCutomBtn];
self.navigationItem.leftBarButtonItem = exampleButton;