如何通过swift代码更改初始视图控制器

时间:2016-03-29 12:32:39

标签: ios swift2 viewcontroller

我正在开发一个带有初始视图控制器的应用程序作为"导航控制器"。该应用程序包含2个实现的想法。

1)从RegisterViewController,用户必须通过sms或调用来验证移动号码//验证后进入HomeViewController(条件:用户第一次安装应用程序时显示RegisterViewController) enter image description here

2)如果应用程序已经安装到iPhone并且用户也注册了手机号码,那么用户现在打开相同的应用程序,而initialViewController必须是HomeViewController

enter image description here

如何通过快速代码实现这一想法?

3 个答案:

答案 0 :(得分:2)

在appdelegate类中实现此逻辑:

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

        let isRegistered = NSUserDefaults.standardUserDefaults().boolForKey("ALLREADY_REGISTER")

        if isRegistered == true{
            // implement home view controller
            let homeViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("HomeVC") as! HomeViewController
            self.window?.rootViewController = homeViewController
            self.window?.makeKeyAndVisible()

        }else{
            // implement register view controller
            let registerViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("RegisterVC") as! RegisterViewController
            self.window?.rootViewController = registerViewController
            self.window?.makeKeyAndVisible()
        }

                return true
    }

然后,当第一次成功注册成功完成后,为键ALLREADY_REGISTER

设置bool变量true
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "ALLREADY_REGISTER")`

答案 1 :(得分:1)

喜欢

  var viewController: UIViewController!


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


self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

    if NSUserDefaults.standardUserDefaults().objectForKey("notRegistered") == nil
    {
        viewController = storyboard.instantiateViewControllerWithIdentifier("RegisterViewController")
    }
    else {

        if NSUserDefaults.standardUserDefaults().objectForKey("notRegistered") as! String == "registred"
        {
             viewController = storyboard.instantiateViewControllerWithIdentifier("HomeViewController")
        }
        else
        {
            viewController = storyboard.instantiateViewControllerWithIdentifier("RegisterViewController")
        }
      // if you want to create the UINavigationController use this
        let nav = UINavigationController(rootViewController: viewController)
        self.window!.rootViewController = nav

      else directly access use this
        self.window!.rootViewController = viewController


self.window.makeKeyAndVisible()
// Override point for customization after application launch.
return true
}
成功保存后

在注册页面上

//Save
NSUserDefaults.standardUserDefaults().setObject("registred", forKey: "notRegistered")

答案 2 :(得分:0)

您必须更改rootViewController而不是initialViewController。当用户验证并在NSUserDefaults中输入HomeViewController商店标志时,每次AppDelegate didFinishLaunchingWithOptions检查用户都已使用NSUserDefaults在HomeViewController中输入。

let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var homeViewController = mainStoryboard.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
let nav = UINavigationController(rootViewController: homeViewController)
appdelegate.window!.rootViewController = nav