Pop-up modal with UITableView on iPhone (follow) - iphone

I've read the post : Pop-up modal with UITableView on iPhone
and I don't understand the following part of the answer (as I can't comment the original post, I create this new question) :
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:optionViewController];
Why allocate a new controller as the window from where the new optionController is called may already have one ?
What if I just write :
OptionViewController* optionViewController = [[OptionViewController alloc] initWithNibName:#"OptionView" bundle:nil];
[self.navigationController presentModalViewController:optionViewController animated:YES];
It seems to work...
If I have a list, that goes to a detail View, from where I switch to a modify view, and then from where I call this option window, what would be the code to use to call this optionWindow ? This one ? Any other one ? I really have a problem dealing with UINavigationController between screens... (where should be defined the first one, what should be passed between screens, when may I create a new one, ...)

Modal views don't use the UINavigationController of their parent. This means that if you need a "stack" of new view controllers in your modal view then you'll need to add your own. On the other hand, if you don't need the functionality of a navigation controller in your modal view then there's no reason to add one.
Here are sone more details of how I did it in two of my apps:
My root view controller has a UINavigationController. I open a modal view using this code:
TwitterPostViewController* vc = [[TwitterPostViewController alloc] init];
[viewc presentModalViewController:vc animated:YES];
[vc release];
The modal view is then dismissed using this code:
[self dismissModalViewControllerAnimated:YES];

Related

Presenting two nested modal view Controllers but show just the last transition

I have a 1st VC (view controller) that should be modal, and it has a child modal VC that sometimes should be presented as soon as 1st VC will appear, but sometimes not.
So from a root VC I want to present 1st VC, and over the 1st VC present the child modal VC. But the user should only see the child VC modal transition (not the 1st VC).
AFAIK the 1st VC can only present a modal VC after viewDidAppear:, so I don't know how to make this possible, since when viewDidAppear: is called, the 1st VC is already visible to the user.
Don't want the user to see 2 modal transitions one after the other, but just the last modal transition, the child's one.
Any hint?
I figured out the simplest solution to this if you still haven't found a suitable one. You can use a UINavigationController to hold the 2 nested view controllers you are trying to display modally.
In the function that is meant to display the modal views you could do something like:
- (IBAction)showView3
{
ViewController2 *new2 = [[ViewController2 alloc] init];
ViewController3 *new3 = [[ViewController3 alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:new2];
nav.navigationBarHidden = YES;
[nav pushViewController:new3 animated:NO];
[self presentModalViewController:nav animated:YES];
}
Then function in ViewController3 to dismiss it would have:
[self.navigationController popViewControllerAnimated:YES];
And the one in ViewController2 would have:
[self dismissModalViewControllerAnimated:YES];
The only issue I can see with this is aesthetics, as by default the transition from view3 to view2 is a horizontal animation but the one from view2 back to view1 is vertical. You could of course change that as well to make them all horizontal, or all vertical, or however you want.
You could have 1 modal view controller with 2 views. Then just pick which view you want to display when the view controller loads.
You should be able to put presentModalViewController anywhere you want, including viewWillAppear.
[self presentModalViewController:myModalViewController animated:NO];
edit: for anyone reading this, see my other (correct) answer, because this one isn't.

How to chain modal views from a controller launched from UITabBarController

How would I go about chaining several modal controllers from a UITabBarController's view? The View Programming Guide from Apple says this is feasible but when I attempt such a task, I get the error,
"*Assertion failure in -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UIWindowController.m:186
The Class hierarchy is something like this:
UITabBarController -> 1 child is a UIViewController inherited class named, Tab1Controller.
Tab1Controller -> orchestrates each of the 2 controllers that need to be presented modally.
Launches 1 modal UIViewController and when this finishes up (get called via a callback), dismisses it and then initiates another modal UIViewController.
It's as if there's not enough time between the two modal controllers ending and starting.
Is there any sample code that shows how to have one modal controller after another can be chained?
See my answer to this SO question:
Correct way of showing consecutive modalViews
It's as if there's not enough time between the two modal controllers ending and starting.
I think you've hit the nail on the head there. You cannot present a new modal view controller until the previous one has finished disappearing and the viewDidAppear: method is called on the view controller that had been being covered by the old modal view.
Another option would be to present the second modal view on top of the first, e.g.
[firstModalViewController presentModalViewController:secondModalViewController animated:YES]
You can then call [firstModalViewController dismissModalViewControllerAnimated:YES] to dismiss the second (returning to the first), or [self dismissModalViewControllerAnimated:YES] to dismiss both at once.
// present modal view inside another presented modal view
FirstViewController *firstVC = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: firstVC];
// Note: you can use your viewcontroller instead self.window.rootViewController
[self.window.rootViewController presentViewController:navController animated:YES completion:^{
//code...
SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[navController presentViewController: secondVC animated:YES completion:nil];
}
}];

presentmodalviewcontroller navigationcontroller

I am creating a Navigation based iPhone application.
In that I have called a UiViewController using presentModalViewController. After that, the ViewController becomes visible. From that ViewController I need to call another ViewController using the sample presentModalViewController. Is this possible or not?
What do you mean by "call another uiviewcontroller"? (It really helps if you can be more detailed in your question.) If you mean, "slide in another view controller", then:
MyNewViewController *myNewViewController = [[MyNewViewController alloc] initWithNibName:#"MyNewViewController" bundle:nil];
[navigationController pushViewController:myNewViewController animated:YES];
[myNewViewController release];
...where:
MyNewViewController is the new view controller class that you want to slide in (the above code assumes you have an XIB file for the view controller class).
navigationController points to the current navigation controller. You'll have to replace it with something like [self navigationController], depending where you are in the view hierarchy.
U might be using following line to present a view controller.
//assume name of viewController which u want to present is "myViewController"
[self.navigationController presentModalViewController:myViewController animated:YES]
If you want to push an other ViewController or present an other ViewController then u will need to replace above line with following lines.
//[self.navigationController presentModalViewController:myViewController animated:YES];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
navigationController.navigationBarHidden = YES; //if u want to show navigation bar then remove this line
[self presentModalViewController:navigationController animated:YES];
After using above code you can present or push other view controllers within presented view controller.
Hope it will solve your problem :)

iPhone modal view inside another modal view?

My App uses a modal view when users add a new foo. The user selects a foo type using this modal view. Depending on what type is selected, the user needs to be asked for more information.
I'd like to use another modal view to ask for this extra information. I've tried to create the new modal view like the first one (which works great) and it leads to stack overflow/“Loading Stack Frames” error in Xcode.
Am I going about this in completely the wrong way i.e. is this just a really bad idea? Should I rethink the UI itself?
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:addController];
[self presentModalViewController:navigationController animated:YES];
Fixed. I got the behavior I wanted by pushing the second view controller to the first view controller's UINavigationController.
creation of 1st modal view
FooAddController *addController = [FooAddController alloc]
initWithNibName:#"FooAddController" bundle:nil];
addController.delegate = self;
addController.foo = newFoo;
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:addController];
[self presentModalViewController:navigationController animated:YES];
[addController release];
creation of 2nd modal view (in FooAddController)
FooAddSizeViewController *addSizeController = [[FooAddSizeViewController alloc]
initWithNibName:#"FooAddSizeViewController" bundle:nil];
addSizeController.delegate = self;
addSizeController.foo = self.foo;
[self.navigationController pushViewController:addSizeController animated:YES];
[addSizeController release];
You need to take care on which instance you invoke the presentModalViewController when you deal with several levels of modal controllers.
Let's suppose you have :
[myControllerA presentModalViewController:myControllerB animated:YES];
Next time you want to display a modal controller while B has the focus, you should invoke
[myControllerB presentModalViewController:myControllerC animated:YES];
in order to get the parent controller properly set.
The hierarchy of controllers is then A-> B -> C
Did you try calling presentModalViewController on self.navigationControllerin both steps?

UINavigationController and modalViewController problems

Hi I'm having this problem:
I have a UITabBarController with UINavigationControllers in each tab.
However, I'm trying to implement an action that, when I click on a button, should present me a new view with a UINavigationController (since it will be multi-view) in a modal way.
What I've tried is to implement a new UIViewController, with a NavigationBar. And then on the button handler I wrote something like this:
SendMessageViewController *v = [[SendMessageViewController alloc] initWithNibName:#"SendMessageView" bundle:nil];
UINavigationController *t = [[UINavigationController alloc] initWithRootViewController:v];
[(UINavigationController *)[tabbar.viewControllers objectAtIndex:0] presentModalViewController:t animated:true]; // hardcoding 0, I know
However, the modal view that's pushed appears with 2 navigation bars, one empty, and my other defined on the "SendMessageView" XIB.
Is there a way to solve this? I don't want to create a XIB just to contain a UINavigationController.. I thought I could do it programatically.
What you want to do is this:
[tabBarController presentModalViewController:t animated:Y];
Basically, get a handle to the tab bar ViewController, and then you'll have the single nav bar you were seeking.