Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

gwelmarten

macrumors 6502
Original poster
Jan 17, 2011
476
0
England!
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
 

gwelmarten

macrumors 6502
Original poster
Jan 17, 2011
476
0
England!
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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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:
Code:
- (IBAction)buttonPressed:(id)sender {
    VC2 *vc2 = [[VC2 alloc] init];
    [self presentViewController:vc2 animated:YES completion:nil];
}
 

gwelmarten

macrumors 6502
Original poster
Jan 17, 2011
476
0
England!
Show us the code. Because if you do something like this inside your IBAction, your modal view should present as "new" every time:
Code:
- (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:

Code:
-(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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Code:
-(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

macrumors 6502
Original poster
Jan 17, 2011
476
0
England!
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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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

macrumors 6502
Original poster
Jan 17, 2011
476
0
England!
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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.