简体   繁体   中英

Switching between view controllers

I have three view controllers. I used the flipsideproject template and then added another view controller.

There is a button on the first view controller that goes to the second view controller. There is a button on the second view controller that goes back to the first one. When switching between the first and second, those buttons always work.

It is the same situation with the second and third view controller. When I try to transfer between the first to second to third and then back to first, it does not work.

(1-->2-->3-->2-/->1) My poorly drawn diagram depicts the situation.

I had all of the back buttons connected to the back IBAction, which I thought was the problem. I then made another IBAction, but it has not fixed the problem.

1st view controller = MainViewController 2nd VC = FlipSideViewController 3rd VC = ChooseAlarmSound

This is for going 2->1 (this is the problem I think. It sometimes works)

- (IBAction)done:(id)sender 
{ 
   [self.delegate flipsideViewControllerDidFinish:self]; 
} 

This is for going 2->3

- (IBAction)chooseSound:(id)sender 
{ 
    ChooseAlarmSound *controller = [[[ChooseAlarmSound alloc] initWithNibName:@"ChooseAlarmSound" bundle:nil] autorelease]; 
    controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
    [self presentModalViewController:controller animated:YES]; 
}

This is for going 3->2

- (IBAction)goBack:(id)sender 
{ 
    FlipsideViewController *controller = [[[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil] autorelease];
    controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
    [self presentModalViewController:controller animated:YES]; 
}

You presented your 3rd VC (going from 2 to 3) using modalviewcontroller. But then you tried to go back to 2nd VC (from 3rd to 2nd) using another modalVC. That will not let you go back to the previous instance of 2nd VC. You need to use dismissmodalviewcontrolleranimated method to do this. Checkout Apple website on modalviewcontroller class reference for detail info on this.

As suggested by user523234, all you need to do is call

[self dismissModalViewControllerAnimated:YES]

in the

- (IBAction)goBack:(id)sender 

method of the 3rd view controller, instead of what you're doing, which is creating another instance of the 2nd view controller and presenting it.

The reason it's not working now, is because when you press the done button in the 2nd view controller it calls

 - (IBAction)done:(id)sender 
{ 
   [self.delegate flipsideViewControllerDidFinish:self]; 
}

which is sending a message the 2nd view controller's delegate, which you haven't set in the case where you're going from 3->2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM