快速设置初始viewcontroller

时间:2019-11-25 00:48:53

标签: ios swift iphone xcode

如果用户的钥匙串中有令牌,则尝试设置初始视图控制器。

当我尝试像这样在appDelegate中进行设置

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            let accessToken: String? = KeychainWrapper.standard.string(forKey: "accessToken")
    if accessToken != nil
    {
        // Take user to a home page
        let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let homePage = mainStoryboard.instantiateViewController(withIdentifier: "home") as! ViewLoader
        self.window.rootViewController = homePage
    }
    return true
}

但是我收到一些错误消息,提示“类型'AppDelegate'的值没有成员'window'”

它试图清理项目但没有运气

2 个答案:

答案 0 :(得分:0)

发生错误的原因是您未声明变量窗口。

在AppDelegate类中添加以下代码是解决错误的方法:

class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let homePage = mainStoryboard.instantiateViewController(withIdentifier: "home")
    self.window?.rootViewController = homePage
    self.window?.makeKeyAndVisible()

    return true
  }
  // ..... more code ...
}

答案 1 :(得分:0)

尝试一下:

window = UIWindow(frame: UIScreen.main.bounds)

let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homePage = mainStoryboard.instantiateViewController(withIdentifier: "home") as! ViewLoader
window?.rootViewController = homePage

window?.makeKeyAndVisible()

首先打开窗口,然后添加功能makeKeyAndVisible