Problem over-riding the Back button in iOS

陌路散爱 提交于 2019-12-11 16:35:55

问题


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?


回答1:


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:)];



回答2:


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.




回答3:


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;


来源:https://stackoverflow.com/questions/7342626/problem-over-riding-the-back-button-in-ios

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