PDA

View Full Version : Resetting a Modal View every time it's opened




gwelmarten
Sep 21, 2012, 10:44 AM
Hi

I've got a View Controller that I open as a Modal View when a button is pressed. This contains some interactive elements, which consist of interface builder objects that move around the screen from their original positions.

I need everything to start from scratch every time the modal view controller i opened. Almost as if closing the view controller close the view completely. Is there a way of doing this?

I know I could simply put into viewWillDisappear the code for sending all the elements back to their original places and removing all the relevant subviews from the view, but I want to find an easier solution. Does anybody have any ideas?

Sam



dejo
Sep 21, 2012, 11:17 AM
How/where are you instantiating and presenting your modal view?

gwelmarten
Sep 21, 2012, 11:19 AM
How/where are you instantiating and presenting your modal view?

For the purpose here, lets say I've got VC1 which called VC2 in a modal view from an UIButton.

I can dismiss the modal view inside VC2 when it's showing.

So I could reset VC2 either from VC1 (which I don't think is possible), or either when VC2 appears or when it disappears.

Sam

dejo
Sep 21, 2012, 11:25 AM
For the purpose here, lets say I've got VC1 which called VC2 in a modal view from an UIButton.

Show us the code. Because if you do something like this inside your IBAction, your modal view should present as "new" every time:
- (IBAction)buttonPressed:(id)sender {
VC2 *vc2 = [[VC2 alloc] init];
[self presentViewController:vc2 animated:YES completion:nil];
}

gwelmarten
Sep 21, 2012, 11:36 AM
Show us the code. Because if you do something like this inside your IBAction, your modal view should present as "new" every time:
- (IBAction)buttonPressed:(id)sender {
VC2 *vc2 = [[VC2 alloc] init];
[self presentViewController:vc2 animated:YES completion:nil];
}

Ah, ok. I was going this, which is different. I see the relevant doc's now as well. I never thought to look at that way of doing things - I just assumed my way was the only way of opening the modal view and I should find a way of resetting. The code I was using was:

-(IBAction)openOneHundredMVC {
OneHundredMViewController *sampleView = [[OneHundredMViewController alloc] init];
[sampleView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:sampleView animated:YES];
}

Out of interest for future use, is there a way of resetting to the original state without leaving the view?

Thanks again so far,

Sam

dejo
Sep 21, 2012, 11:47 AM
-(IBAction)openOneHundredMVC {
OneHundredMViewController *sampleView = [[OneHundredMViewController alloc] init];
[sampleView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:sampleView animated:YES];
}


presentModalViewController:animated: is older but should still present the modal view in a "new" state, if you just instantiated the viewController, which you have. Is that not what you were seeing?

Out of interest for future use, is there a way of resetting to the original state without leaving the view?

Yes, there's a way. But it's not simple and you have to do the work. I believe you would have to keep track of the initial state of all your desired objects and set them back when needed.

gwelmarten
Sep 21, 2012, 11:54 AM
presentModalViewController:animated: is older but should still present the modal view in a "new" state, if you just instantiated the viewController, which you have. Is that not what you were seeing?



Yes, there's a way. But it's not simple and you have to do the work. I believe you would have to keep track of the initial state of all your desired objects and set them back when needed.

Hi dejo
I've tried the code you sent me for show modal view and both yours and mine seem to do the same.
Basically, some of the view is being reset, but not all of it. All the objects get returned to their original locations, however some boolean values are not being reset, along with other variables.

Sam

dejo
Sep 21, 2012, 12:13 PM
Basically, some of the view is being reset, but not all of it. All the objects get returned to their original locations, however some boolean values are not being reset, along with other variables.

Again, show your code. Can you guess which code I want to see? We can't help debug based only on rather vague descriptions and we can't see your screen.

gwelmarten
Sep 21, 2012, 12:23 PM
Again, show your code. Can you guess which code I want to see? We can't help debug based only on rather vague descriptions and we can't see your screen.

I don't see what code it is you want to see. I declare a boolean at the top of my implemtation file, then set it to NO. I then change it after a method has being run to YES. Except that when the user re-opens the modal view, it's already set to YES.

Are you saying you want to see the declaration of the variable? Or the method by which it changes?

dejo
Sep 21, 2012, 12:35 PM
Are you saying you want to see the declaration of the variable? Or the method by which it changes?

All of that... and more. Any code that is run during the initialization and presentation, including any init/initWith... methods, viewDidLoad, viewWillAppear, viewDidAppear (and any supporting methods that they might call). Does it make sense why I want to see all those?