When I push cancel button in the third view, I want to go back to the first view directly.
I also want to remove the second view.
How can I do that?
This is the code.
// this part is in the first view.
self.second = [SecondController alloc] init];
[self.view addSubview:second.view];
// this part is in the second view.
ThirdController *thirdController = [[ThirdController alloc] initWithStyle:UITableViewStyleGrouped];
self.navigationController = [UINavigationController alloc] initWithRootViewController:thirdController];
[self.view addSubview:navigationController.view];
// this part is in the third view.
- (void)cancel {
[self.view removeFromSuperview]; // this only goes to the second view.
}
EDIT:
Can I use popToViewController in called contoller? My app crashes.
I thought popToViewController can be used only in calling controller.
And popToViewController is used when it was pushed.
I did add not push.
popToViewController:animated:is aUINavigationControllermethod that you use when popping view controllers off the navigation controller stack. It doesn’t fit for this scenario.This user is adding subviews, not pushing them on a navigation controller stack.
As a note, it appears as a matter of design you should be using a navigation controller with the first view as the root controller, then the second pushed on the stack, and the third pushed on the stack. Then all you have to do is
[self.navigationController popToRootViewControllerAnimated:YES].I think this will work if you want to keep your current architecture:
If you prefer to try the
UINavigationControllerroute, then the easiest path is to create a new project in Xcode and select the type for a Navigation-Based Application or a Master-Detail Application. This will create aUINavigationControllerin a nib and add it to your window. You can then set the root view controller in Interface Builder to your FirstViewController class.If you prefer to create the
UINavigationControllerin code, then that is also possible. I show that below, along with the rest of the code you need, regardless of whether you create yourUINavigationControllerin a nib in IB or in code.I also recommend reading the View Controller Programming Guide for iOS.
In your app delegate or some other code:
In the first view controller:
In the second view controller:
In the third view controller: