backBarButtonItem doesn't call action

你。 提交于 2019-12-09 21:26:45

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/9765162/backbarbuttonitem-doesnt-call-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!