I have several views that I open one after another modally. View1 calls View2 and View2 calls View3.
I use this code to call the next view:
View2 *myView = [[View2 alloc] initWithNibName:#"View2" bundle:[NSBundle mainBundle]];
[self presentModalViewController:myView animated:YES];
[myView release];
If the user pushes cancel then it goes back one View… 3 to 2 and 2 to 1
[self.parentViewController dismissModalViewControllerAnimated:YES];
What I need to do is when the user is on View3 if they don’t select cancel but complete the operation, then I need to go back to View1 and release View2 and View3.
How do I do that?
EDIT: The MAIN WINDOW has a Navcontroller and 6 view controllers. I call the View 1 like this:
View1 *screen = [[View1 alloc] initWithNibName:#"View1" bundle:[NSBundle mainBundle]];
self.Search = screen;
[mainNavController presentModalViewController:screen animated:YES];
[screen release];
EDIT #2:
Main Windows calls View 1. Main Window has a NavController in the XIB this works:
View1 *screen = [[View1 alloc] initWithNibName:#"View1" bundle:[NSBundle mainBundle]];
[mainNavController presentModalViewController:screen animated:YES];
[screen release];
Then in the XIB on View 1 I added a NavController and tied it to View1NavController in the .h
View 1 then calls view 2:
View2 *myView = [[[View2 alloc] initWithNibName:#"View2" bundle:nil] autorelease];
UINavigationController * navController = [[[UINavigationController alloc] initWithRootViewController:myView] autorelease];
[View1NavController presentModalViewController:navController animated:YES];
When I execute this, no errors, but it doesnt show the View2.
Why don't you use UINavigationController? You can use both popToRootViewControllerAnimated: and popViewControllerAnimated: for your purpose.
As such if you do,
[self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];
You should go back to 1.
Excerpt from dismissModalViewControllerAnimated,
If you present several modal view controllers in succession, and thus build a stack of modal view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.
Using Navigation Controller
For navigation controller to work, instead of where your load your view1 you will do this,
View1 *myView = [[[View1 alloc] initWithNibName:#"View1" bundle:nil] autorelease];
UINavigationController * navController = [[[UINavigationController alloc] initWithRootViewController:myView] autorelease]
[mainNavController presentModalViewController:navController animated:YES];
This is assuming that view1 was the rootViewController
Once you've the navigation controller set up then you can load view2 like this,
View2 *myView = [[View2 alloc] initWithNibName:#"View2" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:myView animated:YES];
[myView release];
In such case,
Cancel
[self.navigationController popViewControllerAnimated:YES];
Complete
[self.navigationController popToRootViewControllerAnimated:YES];
Seems there is no smart way, try this stupid one:
UIViewController *vc = self;
while(vc.parentViewController.modalViewController == vc){
[[vc retain] autorelease];
[vc dismissModalViewControllerAnimated:NO];
vc = vc.parentViewController;
}
Related
I have a UIViewController, I want to navigate from this view to my second view controller
SecondView *secondView=[[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
[self.navigationController pushViewController:secondView animated:YES];
[secondView release];
It doesn't work. It doesn't do anything and there is no error. What I'm missing?
SOLUTION
In Appdelegate file I've added a navigationcontroller.
UINavigationController *navCtrlr = [[UINavigationController alloc]initWithRootViewController:self.viewController];
[self.window setRootViewController:navCtrlr];
navCtrlr.delegate = self;
navCtrlr.navigationBarHidden = YES;
Is your view controller already inside a navigation controller otherwise it wont work. put an NSLog on self.navigationcontroller and see what it is printing
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.
[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:0] animated:YES];
popToViewController:animated: is a UINavigationController method 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:
// this part is in the third view.
- (void)cancel {
// remove the second view (self.view.superview) from the first view
[self.view.superview removeFromSuperView];
// can't recall, possibly you still need to remove the third view, but i think removing the superview will do it.
// [self.view removeFromSuperView];
}
If you prefer to try the UINavigationController route, 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 a UINavigationController in 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 UINavigationController in code, then that is also possible. I show that below, along with the rest of the code you need, regardless of whether you create your UINavigationController in 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:
-(void)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions [
// I would recommend setting up the UINavigationController and FirstViewController as IBOutlets in your nib, but it can be done in code.
FirstViewController* fvc = [[FirstViewController alloc] initWithNibName:#"FirstView" bundle:nil];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:fvc];
[window addSubView:navController.view];
[window makeKeyAndVisible];
[fvc release];
[navController release];
}
In the first view controller:
SecondViewController* svc = [[SecondViewController alloc] initWithNibName:#"SecondView" bundle:nil];
[self.navigationController pushViewController:svc animated:YES];
[svc release];
In the second view controller:
ThirdViewController* tvc = [[ThirdViewController alloc] initWithNibName:#"ThirdView" bundle:nil];
[self.navigationController pushViewController:tvc animated:YES];
[tvc release];
In the third view controller:
-(void)cancel {
// returns to the first view controller
[self.navigationController popToRootViewControllerAnimated:YES];
}
Use
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
to go back to a specific view controller.
Try this:
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
This will pop to the view at index 1. Hope that Helps!
// this part is in the third view.
- (void)cancel {
self.first = [SecondController alloc] init];
[self.view addSubview:second.view];
}
And I think if you have you don't need to be worried about removing beneath view, later these will removed.
I want to push root view controller. Why doesn't this code work?
RootController *rootController = [[RootController alloc]initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:rootController animated:YES];
[rootController release];
I used addSubview like this before.
- (void)cancel {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad { // this is root view controller
[super viewDidLoad];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancel)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
}
RootController *rootController = [[RootController alloc]initWithStyle:UITableViewStylePlain];
UINavigationController *aNavigationController = [[UINavigationController alloc]initWithRootViewController:rootController];
self.naviController = aNavigationController;
[aNavigationController release];
[rootController release];
[self.view addSubview:[naviController view]];
And I added cancel button in navigation bar to go back to previous view. It doesn't work.
So, I want to push instead of add.
You set the root controller in a UINavigationController using the
initWithRootViewController:
method. So, the way you are doing is correct. I would suggest you to inspect self.view and ensure that it is not nil.
EDIT: after your comment
You need to define a root view controller for your UINavigationController to work properly; from the UINavigationController reference:
Every navigation stack must have at least one view controller to act as the root.
So you cannot remove the root view controller. Possibly, to make things work as you like you should create an additional view controller to use as root view controller that you do not alter, then push your RootViewController on the navigation stack, then popping would work:
UIViewController *baseController = [[UIViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:baseController];
self.naviController = aNavigationController;
[aNavigationController release];
[baseController release];
[self.view addSubview:[naviController view]];
RootController *rootController = [[RootController alloc] initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:rootController animated:YES];
[rootController release];
Notice that I first defined a simple UIViewController as root view controller, then pushed your controller on to it.
Once you do this, if you add the cancel button like you do, it will work popping the rootViewController from the navigation stack.
AFTER LAST COMMENT:
If I understand you right, when clicking on the cancel button, you want to get rid of the UINavigationController altogether.
In this case, use the following code for cancel:
- (void)cancel {
[self.navigationController.view removeFromSuperview];
}
If this guess is right, keep in mind that since you are not keeping any reference to the navigation controller, it will be deallocated and with it all the view controllers you instantiated.
If instead of removing the UINavigationController altogether, you would simpy hide the navigation bar, then after popping rootController, call:
setNavigationBarHidden:animated
I am working with push notifications. i am trying to create and push a DetailView in the navigationController when action button in notification is clicked. but navigationController is nil. how can i put that DetailView in the navigationController? I want to push RootViewController in the navigationController and then the DetailView. how can i do that?
in AppDelegate:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
RootViewController *controller = [[RootViewController alloc] init];
//getting warnings here.(Unused variable navigationController)
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
[controller doStuff];
[controller release];
}
in RootViewController:
-(void)doStuff{
[[self stories] removeAllObjects];
[self startParsing];
[self.tableView reloadData];
DetailViewController *detail = [[DetailViewController alloc] init];
//custom code
[self.navigationController pushViewController:detail animated:YES];
[detail release];
this is the code i m using right now. and plz notice that i have
[self.tableView reloadData];
Okay, now I am pretty sure I understand the issue. The problem is, you never manually set the navigationController property on a UIViewController. The navigationController property is nil if the view controller is not under a navigation controller and if it is, then the property points to it.
What you need to do, is when you display your root view controller, instead of directly displaying its view, add it to a navigation controller, then display the navigation controller's view. Like so:
RootViewController *controller = [[RootViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
//Here you would display navigationController.view somehow
So, after you have your root view controller in a navigation controller, in a method in root view controller you can do this:
DetailViewController *detail = [[DetailViewController alloc] init];
//Do whatever you need to do to set values on the detail view controller
[self.navigationController pushViewController:detail animated:YES];
The key thing is this: you need to put the root view controller into a navigation controller before you can access a navigation controller from within root view controller.
If your nav controller is nil, it needs creating and then you place things into it.
Try this:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[navController pushViewController:detailViewController animated:NO]; // or YES
If you start out with no UINavigationController and want to display it with more than one view controller in its stack, after initing the navigation controller you can set the viewControllers property to an array with the various view controllers you want.
edit: some example code:
UINavigationController *navController = [[UINavigationController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
[navController setViewControllers:viewControllers animated:NO];
After doing that, your navigation controller will have viewController2 on top and viewController1 behind it.
Alternately, you could do it this way:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController1];
[navController pushViewController:viewController2 animated:NO];
Hi I have a modalViewController that I am popping up using
[self presentModalViewController:myController animated:YES];
I have an event occurring within myController which I would like to result in another controller being pushed onto the navigation stack ON TOP OF myController (which again has been presented modally). How can I do this?
I have tried the following from within myController:
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:self];
NewController* n = [[NewController alloc] init];
[navController pushViewController:n animated:YES];
[n release];
This does not work however....
First create your second modalViewController
NewController* new = [[NewController alloc] init];
then create navigaitonController like this
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController: new];
navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
then present your navigationController as modalview controller
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
Here you go. Hope it helps.
If i understand right you want to display new navigation stack on top of modal view.
If it is right I think it won't be possible. Modal view is a top one. Even if you'll push new ViewController to the "parent" navigation stack - it won't be available until you'll quite from your modal view.