Search code examples
iphoneobjective-ceventsbuttonback

iPhone: capturing back button event


Greetings,

I have the following code for capturing the back button's event:

[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"Logout" style:UIBarButtonItemStylePlain target:self action:@selector(doLogout:)]];

And here is my doLogout:

-(void) doLogout:(id)sender{
    NSLog(@"hi");
}

Everything compiles and runs fine, and the back button text is changed to "Logout".

The only problem is that my doLogout function is never called!!!

What can I do? I've been stuck on this for an hour now... ;(

Many thanks in advance,


Solution

  • The official doc:

    When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify the back button. The target and action of the back bar button item you set should be nil. The default value is a bar button item displaying the navigation item’s title.