I have a UITableViewController A that is pushing UITableViewController B onto the stack.
In A i have the code:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Trending"
style:UIBarButtonItemStylePlain
target:self
action:@selector(backButtonClicked:)];
backButtonClicked: is also implemented.
B has the title Trending, but when I click it, it doesn't ever reach backButtonClicked:
Why is this?
Try either setting the delegate:
[navigationController setDelegate:self];
Or using the left button. Sometimes the back button doesn't work with certain views and I have had to use the left button instead:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Trending" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClicked:)];
Also, you can try setting B's button item instead of A:
viewControllerB.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Trending" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClicked:)];
In the Xcode documentation, it states that the backBarButtonItem target and action should be set to nil. So even if you do set it, it's probably a good bet that it will be ignored. You could check out the link below to add custom behaviour to the back button.
Custom Action on Back Button Item
Or you could just do the following in viewControllerB:
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Trending"
style:UIBarButtonItemStylePlain
target:self
action:@selector(backButtonClicked:)] autorelease];
Then also add this to viewControllerB
- (void)backButtonClicked:(id)sender {
[[[self.navigationController viewControllers] objectAtIndex:0] backButtonClicked:sender];
[self.navigationController popViewControllerAnimated:YES];
}
The above method will find the RootViewController and send it the backButtonClicked message. It will then pop the current view controller, which should allow you to emulate the backBarButtonItem. Also you can change which view controller you want to send the message by changing the value in the objectAtIndex method.
try not backBarButtonItem but leftBarButtonItem instead
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Trending" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClicked:)];
self.navigationItem.hidesBackButton=YES;
for me it works like a charm. And don't forget that you didn't release this button - and it can cause memory leak. So you can add autorelease when you assign you button or make it like this
UIBarButtonItem *myBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Trending" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClicked:)];
self.navigationItem.leftBarButtonItem = myBackButton;
[myBackButton release];
self.navigationItem.hidesBackButton=YES;