开发者

UINavigationController back button action problem

开发者 https://www.devze.com 2023-04-07 12:31 出处:网络
I have an UINavigationController and the first view is an UIView. I\'ve setup two buttons and each one pushes another view , both UITableViewControllers.

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 开发者_StackOverflow中文版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...


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.

0

精彩评论

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

关注公众号