我如何设置一个启动画面

问题描述:

后的第一或初始视图控制器有这样的场景:
我如何设置一个启动画面

1)当应用程序启动,应用程序显示闪屏。

2)我需要这个闪屏运行一个简单的逻辑

我感到困惑下列要求:

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叫:LaunchScreen.storyboard

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

文件对话框:选择用户界面文件类型:启动屏幕

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

2)添加CocoaTouch文件,并调用它SplashVC.swift

3)Splash.Storyboard

4本SplashVC.swift分配到的UIViewController )单击此的UIViewController并转到文件督察

在Interface Builder的文档

勾去掉:用作LaunchSc颖

5)单击该项目并转到一般

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

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

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的值没有任何部件 '窗口' self.window .rootViewController =的ViewController self.window?makeKeyAndVisible();

请帮忙。谢谢

谢谢

+0

我哈已经创建了单独的splash vc,您可以在其中检查UserDefaults。并根据该条件加载视图控制器。 –

这是非常简单的只是删除发射屏幕故事板,并添加自己的。

只需按照

步骤: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(); 
      } 
     } 
+0

您的splash.storyboard和LaunchScreen.storyboard是相同的。现在,我在Storyboard中有一个MainVC.swift,它也是要加载的第一页。我是否删除了MainVC的箭头?如何从Func CheckLogin加载或导航到VC1或VC2? – MilkBottle

+0

不,我已经删除LaunchScreen.storyboard,因为你不能指定你的课程启动屏幕 –

+0

不要'从主故事板中删除其箭头的箭头 –