I am trying to remove two viewcontrollers (that have been added on top of each other) with one method. I have made the views in interfacebuilder. they all have their own .h and .m files to go with it.
Scenario I am in:
I have a main menu which has the view2 header file imported. In a method I add the second view on top of the superview like so
view2ViewController * view2 = [[view2ViewController alloc] initWithNibName:@"view2ViewController" bundle:nil];
[self.view addSubview:view2.view];
then in view 2 I have added the view 3 header file so i can add view 3 as a subview ontop of view2. i have another method which is connected again to interface builder to a UIButton so upon button press a method gets called in view2 which adds view 3 on top in exactly the same way like so:
view3ViewController * view3 = [[view3ViewController alloc] initWithNibName:@"view3ViewController" bundle:nil];
[self.view addSubview:view3.view];
What im trying to solve: I have a button in view 3 which should remove view 3.... and then it should also remove view 2 aswell so the main screen is visible.
How can this be achieved?
What I have so far:
[self.view removeFromSuperview];
This however only removes View 3... but leaves view 2 in place.
What needs to be modified so that i can remove view 开发者_JAVA百科2 as well??
Any help is appreciated.
Actually the way you have added any view is the same way you'll remove that view. Suppose, you added the view by pushing it into the navigation controller like this
[self.navigationController pushViewController:yourViewController animated:YES];
then you'll need to write this into yourViewController.m file
//inYourViewController.m file
[self.navigationController popViewControllerAnimated:YES];
NEW EDIT :
Ok, so assuming you did presentModalViewController then do this.
Now, your second question where you want to go to view1 instead of view2. Make a boolean variable in your AppDelegate. Set it when you press the done button in View3.
//inYourAppDelegate.h
BOOL doneBtnClicked = NO;
//inYourViewController3.m
- (IBAction) doneBtnPressed : (id) sender
{
//your code
yourAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
appDelegate.doneBtnClicked = YES;
>>CHANGE THIS [self.view removeFromSuperview];
}
Now, in your view2, in the viewWillAppear method
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//your code
yourAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
if(appDelegate.doneBtnClicked)
{
appDelegate.doneBtnClicked = NO;
>>AND THIS [self.view removeFromSuperview];
}
}
HOWEVER, what you want to do is very easily possible with the help of UINavigationController.I suggest you to do with the help of navigation, as it would really become easy in the terms of memory management and moreover Navigation Controller is for this purpose only.
精彩评论