backBarButtonItem doesn't call action

2019-04-15 03:47发布

I would like to call an action when my backButton is clicked but this doesn't seem to be it.

viewDidLoad in rootViewController:

self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] 
                                          initWithTitle:@"Logout" 
                                          style:UIBarButtonItemStyleDone 
                                          target:self 
                                          action:@selector(logout)] autorelease];

The title of it is correct but nothing happens.

logout (in the rootViewController) header:

-(void)logout;

body:

-(void)logout {
     NSLog(@"test");
     [[User owner] logout];
}

Could anyone tell me how to solve this, since i have no idea. Thanks

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-15 04:24

You can change only the title of backBarButton. You can try to use viewWillDisappear or viewDidDisappear functions but they could be called not only after you press your button. The leftBarButton is a better solution but this button's view differs from backBarButton's view.

查看更多
一纸荒年 Trace。
3楼-- · 2019-04-15 04:42

The backBarButtonItem exists specifically to change the appearance of the back button. If you need a custom action, you should consider using leftBarButtonItem instead.

查看更多
登录 后发表回答