2

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

Costique
  • 23,712
  • 4
  • 76
  • 79
chikuba
  • 4,229
  • 6
  • 43
  • 75

2 Answers2

6

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.

Gargo
  • 704
  • 6
  • 16
  • yeah, they sure look different. the "viewDidDisappear" will be called when I push items on the controller aswell right? – chikuba Mar 19 '12 at 20:02
  • I meaned that a program in iOS calls viewDidDisappear or similar functions in several situations (not backBarButton only) but I still haven't tried to check the sender of this event – Gargo Mar 20 '12 at 07:09
3

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

Costique
  • 23,712
  • 4
  • 76
  • 79