IOS 6: is a modal view controller

I have a table view controller and a login controller in xcode 4.6 with iOS 6.1 sdk

enter image description here

When the application starts up, "View Controller 1". How can I show the login window if the user is not logged in? In the viewDidLoad () of the View 1 controller, I paste 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 happens. How can I show the login controller?

thanks for the support

+3


source to share


2 answers


I am using Xamarin Studio 4.0 with Xcode 4.6 and iOS 6.1 and I was able to show the login screen using a storyboard. My code is in C #, but I'm pretty sure you can translate it to the Objective-C equivalent.

When using a storyboard, Window and RootViewController will already be set with a "Start Scene" from the storyboard. So in the AppDelegate I create an instance of my LoginViewController using its "Storyboard Id". Then I 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 created a property to hold the InitialViewController and the Action for the login button. After it is logged in, I reset the RootViewController to the InitialViewController that was cached and then fire 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);
    }
}

      

LoginViewController can be standalone just like in your storyboard. It shouldn't be connected to any other scenes or any segment needed.

+2


source


Here is Objective C code that works for me to do this. Note that in the storyboard, I have a tab bar controller set as the root view controller (ie, I have a check mark next to "Source view controller"). The code overrides this setting to call the standalone login controller instead.

//Note that my storyboard file 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 widget controller, when it is ready to reject itself because the login has been verified to be correct, I call the method in the app's del as follows:

//Be sure to import the App Delegate at the top with #import "AppDelegate.h"
AppDelegate *myAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[myAppDelegate showMainScreen];

      



In the Application Delegate, the showMainScreen method is used here. Note that I am rejecting a login controller that was temporarily set as the root view controller and returned the main screen 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;
}

      

Another tip: I like to pop up the login screen every time the application has been minimized as a security measure, so I call the method applicationWillEnterForeground

in the application delegate as a way to swap the login controller every time:

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [self showLoginScreenIfNecessary];
}

      

0


source







All Articles