Reputation: 4097
i have a tab view controller and a login view controller in xcode 4.6 with iOS 6.1 sdk
When the app start is loaded the "View Controller 1". How can i show the login view if the user is not logged? In the viewDidLoad() of the View Controller 1 i have insert this code:
MyNewAppAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
if(!appDelegate.isUserLogged){
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
LoginViewController *controller = (LoginViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"LoginView"];
[self presentViewController:controller animated:YES completion: nil];
}
But nothing happen. How can i show the Login View Controller?
Thanks for your support
Upvotes: 3
Views: 4732
Reputation: 6092
Here is Objective C code that works for me to do this. Note that on the storyboard, I have the tab bar controller set as the root view controller (that is, I have a checkmark next to "Is Initial View Controller"). The code overrides this setting to make the standalone login view controller popup instead.
//Note that my storyboard file's name is "Main.storyboard"--here you put the name of the storyboard file WITHOUT The extension, which is why I just say "Main" here.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//On the storyboard, you must set the Storyboard ID of the Login View Controller to the name "LoginForm" that is used below, so the code can find the View Controller referred to
UIViewController *loginController = [storyboard instantiateViewControllerWithIdentifier:@"LoginForm"];
self.window.rootViewController = loginController;
In the Login View Controller, when it's ready to dismiss itself because the login has been verified as correct, I call a method in the App Delegate like this:
//Be sure to import the App Delegate at the top with #import "AppDelegate.h"
AppDelegate *myAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[myAppDelegate showMainScreen];
In the App Delegate, here's the "showMainScreen" method. Note that I'm dismissing the login view controller that was temporarily set as the root view controller, and putting the main screen back as the root view controller.
- (void)showMainScreen {
[self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *main = [storyboard instantiateViewControllerWithIdentifier:@"tabBarForm"];
self.window.rootViewController = main;
}
One other tip: I like to pop up the login screen every time the app has been minimized as a security measure, so I call the applicationWillEnterForeground
method in the app delegate as a way to swap in the login controller everytime it comes up:
- (void)applicationWillEnterForeground:(UIApplication *)application {
[self showLoginScreenIfNecessary];
}
Upvotes: 0
Reputation: 1633
I'm using Xamarin Studio 4.0 with Xcode 4.6 and iOS 6.1 and I was able to show a login screen using a storyboard. My code is in C# but I'm sure you can translate it into the Objective-C equivalent.
When using a storyboard the Window and RootViewController will already be set with the "Initial Scene" from the storyboard. So in the AppDelegate I instantiate an instance of my LoginViewController by using its "Storyboard ID". I then cache an instance of the current RootViewController and then set the new LoginViewController as the RootViewController.
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
LoginViewController loginController;
public override UIWindow Window { get; set; }
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
loginController = Window.RootViewController.Storyboard.InstantiateViewController("LoginScene") as LoginViewController;
loginController.InitialViewController = Window.RootViewController;
Window.RootViewController = loginController;
return true;
}
//... other overrides ...
}
Inside the LoginViewController I've created a property to hold the InitialViewController and an Action for the login button. After it does the login work I reset the RootViewController to the InitialViewController that was cached and then dismiss the current LoginViewController.
public partial class LoginViewController : UIViewController
{
public UIViewController InitialViewController { get; set; }
public LoginViewController (IntPtr handle) : base (handle)
{
}
partial void OnLoginClicked(MonoTouch.UIKit.UIBarButtonItem sender)
{
//... do login work here ...
UIApplication.SharedApplication.Delegate.Window.RootViewController = InitialViewController;
DismissViewController(false, null);
}
}
The LoginViewController can be standalone just like you have it in your storyboard. It doesn't need to be connected to any other scenes or need any segues.
Upvotes: 2