How do I present a modal view at application startup? - iphone

I have the following code in application didFinishLaunchingWithOptions where I want to present a modal view controller for user login.
LoginViewController_iPhone *loginViewController=[[LoginViewController_iPhone alloc]initWithNibName:#"LoginViewController_iPhone" bundle:nil];
UINavigationController *loginNavigationController=[[UINavigationController alloc]initWithRootViewController:loginViewController];
loginNavigationController.modalPresentationStyle=UIModalPresentationFullScreen;
[self.window.rootViewController presentModalViewController:loginNavigationController animated:NO];
[loginViewController release];
[loginNavigationController release];
However, all I get is a blank white screen. If I substitute the following
self.window.rootViewController=loginNavigationController;
the login screen displays correctly. There is no other view controller assigned to the rootViewController property as the app is just starting. Do I need another view controller assigned to get this to work?

Yes. you need to assign something to the window's rootViewController property in order to call its method presentModalViewController.

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:LoginViewController];
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:navController animated:NO];
You can set this up in the viewDidLoad of the view that would load up first as soon as the app starts. So, as soon as login is successful, you can pop it off, and you will have the loaded view ready.

Related

Initial screen auto fadeout to app

I am a master detail app which is embedded in tab bar controller. This loads from the beginning and works fine. Now, i want an initial page to load as soon as the app loads and then auto fade out to this screen without any event. How can i do the same? Please advice
You can add the initial viewController in the application:didFinishLaunchingWithOptions: method.
Present your initial controller ontop of the UINavigationController that holds the rootViewController (with no animation).
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:yourRootViewController];
self.window.rootViewController = navigationController;
UIInitialController *initialControlller = [[UIInitialController alloc] init];
initialControlller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
initialControlller.modalPresentationStyle = UIModalPresentationCurrentContext;
[navigationController presentViewController:initialControlller animated:NO completion:nil];
When you are ready to dismiss your initial you can do this (from the UIInitialController class) - with animation (you can set a NSTimer to call this method automatically):
[self dismissViewControllerAnimated:YES completion:nil];

How can I dismiss a View Controller

I am pushing a controller via:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
Once there, I cannot go back with either
[self dismissViewControllerAnimated:YES completion:nil];
or
[self.navigationController popViewControllerAnimated:YES];
what should I use to dismiss the last view ?
To where you want to dismiss and go to?
This is the root view controller right? It cannot be "dismissed".
If you have a another view controller and this VC is pushed or modally presented from the second VC it can be dismissed
Window is the base canvas and VC's are added over it and shown.
Ok from your clarification you have an initial VC with contacts and you are trying to go to next page(DetailsVC) from that contactVC and then dismiss back from there.
What you currently doing is getting the window and making your detailVC as the root VC.This way you cannot dismiss to the contacts VC.Make an instance of the detail VC in the didselectRowAtIndexpath method of contactsVC and push it using navigation controller.
Then you can pop using the method popViewControllerAnimated:
This is an excellent tutorial for you to start on
Your code is not "pushing a controller". That code is essentially what you may find in your app delegate for setting up your one initial window and view.
Calling popViewController only works after you've pushed a controller onto that same navigation controller using pushViewController, which you haven't done.
Calling dismissViewController only works after you've presented a view controller with presentViewController, which again you haven't done.
I finally left the above technique and went for the traditional:
ViewController *detailViewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];

Change UIWindow's presented view from UIViewController method

I have a simple application with two subclasses of UIViewController.
I want to change the view being displayed by my application's UIWindow by calling a method from one of my UIViewController subclasses.
Essentially I'm just messing around and I'm trying to construct a simple test application with a login screen, that after a user enters credentials a main view is presented. I'm not too familiar with the window and view mechanisms of iOS programming, and I'm currently reading http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html#//apple_ref/doc/uid/TP40009503-CH2-SW1 and attempting to learn a bit about it.
If this is for the purpose of a login screen, you should add the main view controller to window directly, and add the login view controller as a modal view inside the main view controller.
Inside applicationDidFinishLaunching...
MainViewController *mainViewController = [[MainView....... // instantiate UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:mainViewController];
[mainViewController release];
[window addSubview:navController.view];
Inside MainViewController
-(void)viewWillAppear:(BOOL)animated
{
LoginViewController *loginVC = .... //instantiate
[self.navigationController presentModalViewController:loginVC animated:NO];
[loginVC release];
}
if login successful,
[self dismissModalViewControllerAnimated:YES];

How to add navigation controller programmatically?

In my app there is requirement that..I have 6 buttons in a nib,
when I press any button a new nib will be loaded into the window according to the button pressed. problem is after loading the new nib If I want to come back to the previous nib (which is having all the buttons) how to add navigation controller?
what I am doing now is while loading the new nib when I pressed the button
objNewViewController = [[NewViewController alloc] initWithNibName:#"NewViewController" bundle:nil];
[self.navigationController pushViewController:objNewViewController animated:YES];
but by this way im not able to load the nib, it's not performing any operation?
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[NewViewController alloc] initWithNibName:#"NewViewController" bundle:nil]];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
And in NewViewController:
USe this to dismiss and get back to previous view.
[[self navigationController] dismissModalViewControllerAnimated:YES];
There is a template in Xcode for a navigation based app. It does everything you describe. Well, very close at least, only the AnotherViewController in -tableView:didSelectRowAtIndexPath: is commented out.

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?