Is it possible to create a UINavigationController within a ModalPopup? - iphone

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.

Related

How to add an UINavigationController to an UIViewController presented as Modal

I have an alternative flow in my app. This flow starts in my firstViewController, then in this view a call my secondViewController like this:
- (IBAction)PressButton:(id)sender {
SecondViewController *second = [[SecondViewController alloc] init];
second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
UINavigationController *nav = self.navigationController;
[nav presentViewController:second animated:YES completion:nil];
}
In my secondViewController I want to push my thirdViewController. But it is not working I tried this ways:
- (IBAction)pressButton:(id)sender {
ThirdViewController *tvc = [[ThirdViewController alloc] init];
UINavigationController *nav = self.navigationController;
[nav pushViewController:tvc animated:YES];
}
When I press the button of secondViewController nothing happens.
What I'm doing wrong ?
I'm using:
OSX 10.8.2
Xcode 4.6
iOS 6.1
You must present the navigation controller modally, and have the second view as the root of that navigation controller. As well as calling presentViewController from the owning view not its parent navigation controller.
- (IBAction)PressButton:(id)sender {
SecondViewController *second = [[SecondViewController alloc] init];
second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:second];
[self presentViewController:navigationController animated:YES completion:nil];
}
Instead of presenting just the second view controller, make sure to present an additional navigation controller.
SecondViewController *secondViewController = [[SecondViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
[[self navigationController] presentViewController:navigationController animated:YES completion:nil];
If you are using storyboard just click on the source view xib, Ctrl+drag to destination (Create Segue), select modal from popup menu.Click on the newly created connection. Add a name for it then in source view controller [self performSegueWithIdentifier:#"Segue Name" sender:self];
If you are trying to get a button to direct you to a different page modally, you can go into the storyboard or xib file. Control click from that button to the view controller you want to go to. and then the popup menu will give you the options of what type of outlet you want to use. Hope this helps

iphone modal view - how to go back to parent parent parent controller

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;
}

How to set up a navigationController by UINavigationController?

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];

NavigationBar hidden in detailViewController when presented modally

I am basically presenting my DetailViewController as modalView.In the modalView navigation bar is never visible, though I am not hiding it any where.Please help!
WriteReviewController *newReview = [[WriteReviewController alloc] initWithNibName:#"WriteReviewController" bundle:nil];
UINavigationController *modalNavController = [[UINavigationController alloc] initWithRootViewController:newReview];
// This is intended to be presented in another view controller class
modalNavController.navigationBar.barStyle = UIBarStyleDefault;
[self.parentViewController presentModalViewController:modalNavController animated:YES];
[modalNavController release];
This code worked for me

How to manage TabBarController on new view after viewController push action

I have a problem building applicatin with tabBarController.
There is no problem doing tabBarController with navigationController if I build it from AppDelegate.
But now I have experienced problem when I want to create new view with tabBarController (3 tabs and each has navigation controllers) after a push from previous navigation controller.
It simply doesnt work.
Here is the code:
MainViewController *mainViewController = [[MainViewController alloc] initWithNibName:#"MainView_iPhone" bundle:nil];
mainViewController.tabBarItem.title = #"First";
UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
DictionariesViewController *dictionariesViewController = [[DictionariesViewController alloc] initWithNibName:#"DictionariesView_iPhone" bundle:nil];
dictionariesViewController.tabBarItem.title = #"Second";
UINavigationController *dictionariesNavigationController = [[UINavigationController alloc] initWithRootViewController:dictionariesViewController];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:mainNavigationController, dictionariesNavigationController, nil];
[self.navigationController pushViewController:tabBarController animated:YES];
There is a problem after view is pushe to "First" controller. Application crashes...
Please for help.
Regards
Borut
What are you trying to do with the following code?
[self.navigationController pushViewController:tabBarController animated:YES];
You said that your app has 3 tabs and each of those tabs have a navigation controller. Therefore, what you should do is to add the navigation controllers to tabBarController.viewControllers (which you did), but then you need to set the tabBarController as the root view controller.
I have done it this way and it works:
registerViewController = [[RegisterViewController alloc] initWithNibName:#"RegisterView_iPhone" bundle:nil];
AppDelegate_Phone *delegatePhone = [[UIApplication sharedApplication] delegate];
[delegatePhone.firstViewController.navigationController pushViewController:registerViewController animated:YES];
Thanks for your help guys.