使用Swift和iOS 8 Storyboard登录屏幕 [英] Login Screen with Swift and iOS 8 Storyboard

查看:130
本文介绍了使用Swift和iOS 8 Storyboard登录屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理iOS应用程序的登录流程时遇到了很多麻烦。我想要实现的故事板的图像低于

I have having a lot of trouble handling the login flow of my iOS app. An image of the storyboard I am trying to achieve is below

我正在尝试实现一个可选的登录屏幕,该屏幕仅在用户首次打开应用程序但尚未登录时显示。目前,我将Tab Bar Controller设置为根视图控制器。但是,我无法弄清楚如何处理这些视图控制器之间的交换。

I am trying to achieve an optional login screen which is presented only when a user first opens the app and has not logged in. Currently, I have the Tab Bar Controller set as the root view controller. I can not figure out how to handle swapping between these view controllers, however.

我试图用以下代码简单地按下登录界面。但是,它不起作用。我相信标签栏控制器不允许推送新视图控制器的问题。

I have tried to simply push the login screen with the following code. However, it does not work. I believe the problem that a tab bar controller is not allowed to push new view controllers.

    func application(application: UIApplication, didFinishLaunchingWithOptions  launchOptions: [NSObject: AnyObject]?) -> Bool {
    //stuff

    if userLoggedIn {
        // Do nothing
    } else {
        //get access to login view
        var storyboard = UIStoryboard(name: "Main", bundle: nil)
        var viewController =    storyboard.instantiateViewControllerWithIdentifier("signupView") as UIViewController;

        // Then push login view
        var rootViewController = self.window!.rootViewController as UITabBarController;
        rootViewController.pushViewController(viewController, animated: true)
    }

    // Override point for customization after application launch.
    return true
}

有没有办法在不推送的情况下切换视图控制器在导航控制器内?关于如何处理这种登录流程的任何建议将不胜感激。

Is there a way to switch view controllers without pushing within a navigation controller? Any advice on how to handle this sort of login flow would be greatly appreciated.

推荐答案

我通过LaunchViewController来实现这一点确定它是否应显示登录视图或主导航控制器。

I achieve this with a "LaunchViewController" that determines if it should present the Login view or the main navigation controller.

将启动视图标记为故事板中的初始视图,并在其中写入逻辑以确定要显示的内容。对于用户来说,该过程通常非常快速且不明显。

Mark the launch view your initial view in the storyboard and write the logic in there to determine which to present. This process is often very quick and unnoticeable to the user.

更新

正如我在评论中所提到的,我已经有所不同方向。现在在App Delegate的应用程序(application:didFinishLaunchingWithOptions :)中方法我执行必要的逻辑来确定哪个视图应该是 rootViewController 并使用以下内容将其设置在那里。

As mentioned in my comment I've gone a different direction. Now in the App Delegate's application(application:didFinishLaunchingWithOptions:) method I perform the logic necessary to determine which view should be the rootViewController and set it there using the following.

extension AppDelegate {

    enum LaunchViewController {
        case Login, Dashboard

        var viewController: UIViewController {
            switch self {
            case .Login: return StoryboardScene.Login.LoginScene.viewController()
            case .Dashboard: return StoryboardScene.Dashboard.initialViewController()
            }
        }

        /// Sets `UIWindow().rootViewController` to the appropriate view controller, by default this runs without an animation.
        func setAsRootviewController(animated animated: Bool = false) {
            let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            let window = appDelegate.window!
            let launchViewController = viewController

            log.info?.message("Setting \(launchViewController.dynamicType) as rootViewController")
            if let rootViewController = window.rootViewController where rootViewController.dynamicType != launchViewController.dynamicType && animated {
                let overlayView = UIScreen.mainScreen().snapshotViewAfterScreenUpdates(false)
                launchViewController.view.addSubview(overlayView)

                UIView.animateWithDuration(0.3, animations: {
                    overlayView.alpha = 0.0
                    },
                    completion: { _ in
                        overlayView.removeFromSuperview()
                });
            }

            window.rootViewController = launchViewController
            window.restorationIdentifier = String(launchViewController.dynamicType)

            if window.keyWindow == false {
                window.makeKeyAndVisible()
            }
        }
    }
}

请注意 StoryboardScene 不是默认类型,我使用 https://github.com/AliSoftware/SwiftGen 生成它。

Note that StoryboardScene is not a default type, I am using https://github.com/AliSoftware/SwiftGen to generate that.

My App Delegate方法看起来像这样。

My App Delegate method looks something like this.

if window == nil {
    window = UIWindow(frame: UIScreen.mainScreen().bounds)
}

if isRestoringState == false {
    if let _ = lastUsedAccount {
        LaunchViewController.Dashboard.setAsRootviewController()
    } else {
        LaunchViewController.Login.setAsRootviewController()
    }
}

使用注销登录方法更新

func handleLogout(notification: NSNotification) {
  LaunchViewController.Login.setAsRootviewController(animated: true)
  lastUsedAccount = nil
}

这篇关于使用Swift和iOS 8 Storyboard登录屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆