IOS 6:提供一个模态视图控制器

时间:2013-02-16 15:27:29

标签: xcode uiviewcontroller ios6

我在xcode 4.6中有一个标签视图控制器和一个登录视图控制器,带有iOS 6.1 sdk

enter image description here

当应用程序启动时加载“View Controller 1”。如果用户未登录,如何显示登录视图? 在View Controller 1的viewDidLoad()中,我插入了以下代码:

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];

}

但没有任何事情发生。 如何显示Login View Controller?

感谢您的支持

2 个答案:

答案 0 :(得分:2)

我正在使用带有Xcode 4.6和iOS 6.1的Xamarin Studio 4.0,我能够使用故事板显示登录屏幕。我的代码在C#中,但我相信你可以把它翻译成Objective-C等价物。

使用故事板时,Window和RootViewController已经使用故事板中的“初始场景”进行设置。因此,在AppDelegate中,我使用其“Storyboard ID”实例化我的LoginViewController实例。然后我缓存当前RootViewController的一个实例,然后将新的LoginViewController设置为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 ...
}

在LoginViewController中,我创建了一个属性来保存InitialViewController和一个用于登录按钮的Action。在完成登录工作后,我将RootViewController重置为缓存的InitialViewController,然后关闭当前的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可以是独立的,就像你在故事板中一样。它不需要连接到任何其他场景或需要任何segues。

答案 1 :(得分:0)

这是Objective C代码,对我来说是有用的。请注意,在故事板上,我将标签栏控制器设置为根视图控制器(也就是说,我在“是初始视图控制器”旁边有一个复选标记)。代码会覆盖此设置,以改为使用独立登录视图控制器。

//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;

在登录视图控制器中,当它已经准备好因为登录已被验证为正确而解雇时,我在App Delegate中调用一个方法,如下所示:

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

在App Delegate中,这是“showMainScreen”方法。请注意,我正在关闭暂时设置为根视图控制器的登录视图控制器,并将主屏幕作为根视图控制器返回。

- (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;
}

另一个提示:我希望每次将应用程序最小化时都会弹出登录屏幕作为安全措施,因此我调用应用程序委托中的applicationWillEnterForeground方法作为交换登录控制器的方法每当它出现时:

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