简体   繁体   English

UIBarbuttonItem的动作不称为ios

[英]action of UIBarbuttonItem not called ios

I am trying to call the UIBarButtonItem when the back button of that controller is pressed.This is my code.I have put the initialization in viewDidLoad and viewWillAppear but the method is not being called. 我试图在按下该控制器的后退按钮时调用UIBarButtonItem 。这是我的代码。我已将初始化放入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 will not add backBarButtonItem with your line as : IT不会将backBarButtonItem与您的行一起添加为:

self.navigationItem.backBarButtonItem = exampleButton;

You have to Provide leftbarbutton as 您必须提供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;

Update 1 更新1

You can provide Custom button with Image created by you, as you want, 您可以根据需要向“自定义”按钮提供由您创建的图像,

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM