How to set up a custom UIViewController after a general UIViewController?
(Final solution at the bottom)
I am trying to set up an iPad app that will have an initial login view. When the user clicks the action it should redirect to the home view. Initially I was setting up a UIViewController to handle the login view. From there I wanted to display the next group of views using a custom view controller.
I am creating a LoginViewController that handles the loginView, adding this view into the window from the AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
loginViewController = [[LoginViewController alloc] init];
[self.window addSubview:loginViewController.loginView];
[self.window makeKeyAndVisible];
return YES;
}
And the interface for my LoginViewController as follows:
@interface LoginViewController : UIViewController {
UIView *loginView;
UITextField *usernameTextField;
UITextField *passwordTextField;
UIButton *submitButton;
}
@property (nonatomic, retain) IBOutlet UIView *loginView;
@property (nonatomic, retain) IBOutlet UITextField *usernameTextField;
@property (nonatomic, retain) IBOutlet UITextField *passwordTextField;
-(IBAction)submit;
-(void)setupTextFields;
@end
What I was trying to do was add a HomeViewController, which would extend UIViewController class, inside my LoginViewController so that when the action button inside my loginView is pressed it would load the new custom view controller.
But when I do it that way, the initial displayed image handled by the HomeViewController is rendered incorrectly. It appears to display landscape when it should be portrait and viceversa.
I want to have the login view displayed at the bottom at the stack of views in order to be able to come back to this login view when a logout action is clicked.
I think my approach to this problem is wrong. Can anyone orient me into the best possible solution? How can I accomplish this?
After following the solution proposed by @Jonah, I have switched the homeViewController to be the one created in the AppDelegate and called the displayLoginModalViewController
as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
homeViewController = [[HomeViewController alloc] init];
[homeViewController displayLoginModalViewController];
开发者_StackOverflow社区 [self.window addSubview:homeViewController.homeView];
[self.window makeKeyAndVisible];
return YES;
}
The implementation of my displayLoginModalViewController
as follows:
-(void)displayLoginModalViewController{
LoginViewController *loginViewController = [[LoginViewController alloc] init];
loginViewController.modalPresentationStyle = UIModalPresentationFullScreen;
loginViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:loginViewController animated:YES];
}
I did attempt to call the displayLoginModalViewController
method before and after adding the homeView
to the window and it is not rendering the modal view. I feel like there is something I am missing here. The method does render the modal view when the logout action is pressed from the homeView, just not when the app is initiating.
I also placed NSLog
s after adding the subview in the didFinishLaunchingWithOptions:
and after the presentModalViewController
: in displayLoginModalViewController
method. Both NSLog
s are displayed in my console, I just don't know why it is not displaying the Modal View.
============================================================================================
Thank you for the options provided. I decided to go the modal route. Unfortunately when I was initially setting it up I was approaching the problem the wrong way.
When it comes down to modal views, apparently they require user interaction. This means that they can only be presented after an action done by the user. When I first wanted to set up login as a modal view and keep my home view as my main view, it just wasn't possible to have the modal view display at launch. It required an action.
After staring at the screen for a few hours, I decided to stop being stubborn and use the login as my main screen and display the home view as a modal. This not only solves the issue of displaying login on startup but also helps with the set up I wanted to be able log a user out of the app without exiting the app and simply releasing all memory usage from that point.
You should use a UINavigationController instead. Most apps take this approach, even though the navigationBar is hidden, the views are skinned or it does not animate in from right, there is often a UInavigationController in there.
It takes care of the structure of pushing viewControllers on top of each other, memory, displaying etc. The alternative is to do what the UINavigationController does manually. This would involve your viewController having a variable to store the next viewController and then adding the second viewControllers view to the first viewController ...(it is way to much work so try to avoid that approach being your foundation)
This is a must read. I goes through the different viewController containers and when to use them - it is just the thing you need before things get really complicated. ViewController Programming Guide
Log in views are a natural fit for modal view controllers. Take a look at UIViewController's -presentModalViewController: as a mechanism for presenting your login view controller "on top of" whatever other view controller was currently filling your window.
精彩评论