开发者

How to handle backBarButtonItem pressed?

开发者 https://www.devze.com 2023-04-04 13:41 出处:网络
I have almost done this in all the application but I have 3 views stacked in navigationController and 开发者_JAVA百科I need to jump from the third view to the first view.

I have almost done this in all the application but I have 3 views stacked in navigationController and 开发者_JAVA百科I need to jump from the third view to the first view. As I understand I can do this via viewWillDisappear only. But if I try this "jump" I will get the navigationController panel from the second View which with a navigation buttons which cause exceptions/errors.

P.S. Do not advice me to make leftBarButtonitem looking like backBarButtonItem. It is too difficult and I don't know where to find an appropriate image for it.


To my knowledge, you have no choice but to provide your own UIBarButtonItem. You are not permitted from interrupting how UINavigationController works by default. That is, you cannot override the behavior of the back button. You must provide a custom bar button item and set it as the navigation item's left bar button item.

(As a side note, the sort of behavior you're looking for may be an indication of a poor navigation pattern. Back buttons should almost always back out of a navigation hierarchy sequentially.)


Let's say in navigation order your views stacked like top -> 3 -> 2 -> 1 . When you are in this position you can have a flag in your application delegate that shows you will doublePop when backButton pressed as below: ( You are doing this whenever third view appears in the order you mentioned)

MyApplicationDelegate * del = [[UIApplication sharedApplication]delegate];
del.doublePopEnabled = YES;
[del release];

In view 2 :

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
         MyApplicationDelegate * del = [[UIApplication sharedApplication]delegate];
         if(del.doublePopEnabled){
           //Asssuming you have a reference to your navigationController in your view 2
           del.doublePopEnabled = NO;
           [del.release]
           //Use animated as no if you don't want user to see doublePopping.
           self.navigationController popViewControllerAnimated:NO];

          }
 }

Hope it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消