UINavigationController back button action problem

2019-08-10 03:45发布

问题:

I have an UINavigationController and the first view is an UIView. I've setup two buttons and each one pushes another view , both UITableViewControllers. The first buttons works just fine. The problem is: when I'm in the second TableViewController and I tap on the back button it tries to push the view again and only the second time it goes back to the Main View. I also have an UITabBarController setup and I noticed that if I am in my first tab(the Main View with the two buttons pushing the two tableviews and precisely in the second view ) and i tap on another tab then tap back on the first - it shows me the content of my first UITableViewController and when I tap back it shows the second UITableViewController(that is supposed to be displayed) and only the second time i tap it goes to the Main View.

I don't know if it's understandable what I just said, here is also the code for the back button and the action for it:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *back= [UIButton buttonWithType:UIButtonTypeCustom];  
    UIImage *backImage = [[UIImage imageNamed:@"backb.png"]  
                          stretchableImageWithLeftCapWidth:10 topCapHeight:10];  
    [back setBackgroundImage:backImage forState:UIControlStateNormal];  
    [back addTarget:self action:@selector(cancel:)  
   forControlEvents:UIControlEventTouchUpInside];  
    back.frame = CGRectMake(0, 0, 37, 26);  
    UIBarButtonItem *cancelButton = [[[UIBarButtonItem alloc]  
                                      initWithCustomView:back] autorelease];  
    self.navigationItem.leftBarButtonItem = cancelButton;
}


-(IBAction)cancel:(id)sender{  
    [self.navigationController popViewControllerAnimated:YES];  
}

The back button in the first UITableViewController is setup the same and works just fine...what could be the problem?

I've just added another UITableViewController and obviously now when I'm in the third view and I try to go back to the Main View it loads the view two times and the first UITableViewController before it goes back...

回答1:

There are chances that you have pushed the second view controller twice. While you push multiple instances of view controllers at the same time, you won't see any difference in the pushing animation. It'd look like only one controller is being pushed. This is because you push the next view controller before the previous view controller was pushed completely.

Check your code if you are pushing the second view controller twice.