简体   繁体   中英

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 *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 :

self.navigationItem.backBarButtonItem = exampleButton;

You have to Provide leftbarbutton as

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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