如何在启动画面后设置第一个或初始ViewController

时间:2017-10-13 03:47:34

标签: swift3

我有这种情况:

1)当应用程序启动时,应用程序会显示SplashScreen。

2)我需要这个splashScreen来运行一个简单的逻辑

我对以下内容感到困惑:

Problem:

How to make SplashScreen with logic?
I add a new CocoaTouch file and call it SplashVC.swift 
and assign it to the UIViewController in the LaunchScreen.storyboard.

**at LaunchScreen.storyboard:**

I see there is an arrow on the left of the ViewController. 
This means it is the first VC to be loaded.

**at Main.storyboard**

I have a MainVC which I made it as the first Page to be loaded.

There are two VC which are the First Page to be loaded. 

那么,如何为LuanchScreen执行以下任务:

条件:
  如果UserDefaults中有值,请转到VC1
  如果UserDefaults中没有值,则转到VC2

                          |- viewLogin
   SplashScreen.swift ----|
                          |- viewMain

更新
1)删除名为:LaunchScreen.storyboard

的默认LaunchScreen

1.1)在项目中添加接口文件

在文件对话框中:选择类型为“启动屏幕”的用户界面文件

将其命名为:splash(它将创建splash.storyboard)

2)添加一个CocoaTouch文件并将其命名为SplashVC.swift

3)将此SplashVC.swift分配给Splash.Storyboard中的UIViewController

4)单击此UIViewController并转到文件检查器

在Interface Builder Document

取消勾选:用作LaunchScreen

5)单击项目并转到常规

  Goto App Icons and Launch Images section <br/>
  Launch screen file : Splash.storyboard<br/>

6)我有viewControllers我想导航到基于条件的splashVC.swift:

viewLogin.swift
   viewMain.swift

7)在SplashVC.swift中添加代码

  func checkLogin()
        {
         var mainView: UIStoryboard!
        mainView = UIStoryboard(name: "MainStoryboard", bundle: nil)
        var viewcontroller : UIViewController


             if UserDefaults.standard().bool(forKey: "login"){

viewcontroller = mainView.instantiateViewController(withIdentifier: "viewLogin") as! viewLogin

              }else {
                    viewcontroller = mainView.instantiateViewController(withIdentifier: "viewMain") as! viewMain
              }

//--- error : value of type SplashVC has no member 'window'


        self.window!.rootViewController = viewcontroller
        self.window?.makeKeyAndVisible(); 
            }
        }

错误:

// ---错误:SplashVC类型的值没有成员&#39;窗口&#39;               self.window!.rootViewController = viewcontroller             self.window .makeKeyAndVisible()?;

请帮忙。感谢

由于

1 个答案:

答案 0 :(得分:0)

只需删除启动画面故事板并添加自己的故事板即可。

按照

进行操作
  

步骤:1使用单视图控制器添加您自己的故事板并分配   它是你自己的splashvc课程。

enter image description here

  

步骤:2将您的启动故事板作为主界面

enter image description here

现在你可以像在

那样进行任何编码
 class SplashVC: UIViewController {

            override func viewDidLoad(){
                super.viewDidLoad();
                checkLogin()
            }
            override func viewWillAppear(_ animated: Bool)
            {
                super.viewWillAppear(animated)

            }
        func checkLogin()
        {
         var mainView: UIStoryboard!
        mainView = UIStoryboard(name: "MainStoryboard", bundle: nil)
        var viewcontroller : UIViewController;

             if   UserDefaults.standard().bool(forKey: "login"){
                  viewcontroller   = mainView.instantiateInitialViewController()!;
              }else {
                    viewcontroller   = mainView.instantiateViewController(withIdentifier: "yourSecondVcIde") as! YourSecondVC
              }
self.window!.rootViewController = viewcontroller
        self.window?.makeKeyAndVisible(); 
            }
        }