gpt4 book ai didi

iOS : getting crash while choosing that see pageviewcontroller for first time that apps loaded

转载 作者:行者123 更新时间:2023-11-28 12:17:57 25 4
gpt4 key购买 nike

嗨,我按照堆栈中的一些教程只看到 UIPageviewcontroller 第一次运行我找到了最好的一个,而不是在应用程序委托(delegate)中实现它....首先我只有一个 Storyboard两个 viewcontroller 其中第一个是我的 PageViewController 第二个是我的 Loginvc我在 appdelegate 上实现了这段代码,但我崩溃了,一切看起来都不错,但我崩溃了,这是我实现的代码

var window: UIWindow?
var story : UIStoryboard?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarStyle = .lightContent

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

let lunchedBefore = UserDefaults.standard.bool(forKey: "lunchedBefore")

if lunchedBefore {
story = UIStoryboard(name: "TShopUI", bundle: nil)
let rootcontroller = story?.instantiateViewController(withIdentifier: "LoginVC")
if let window = self.window {
window.rootViewController = rootcontroller
}
} else {

UserDefaults.standard.set(true, forKey: "lunchedBefore")
story = UIStoryboard(name: "TShopUI", bundle: nil)
let rootcontroller = story?.instantiateViewController(withIdentifier: "MainVC")

if let window = self.window {
window.rootViewController = rootcontroller
}

}
return true
}

谢谢你的帮助请尽可能清楚地回答我是 iOS 开发的新手

最佳答案

它因为 UserDefaults 而崩溃,你必须像下面那样处理 UserDefaults

更新:我们可以使用object 代替Bool。所以无论if条件是否满足,都不是第一次启动。 else 首次启动。

var window: UIWindow?
var story : UIStoryboard?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarStyle = .lightContent
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
if let lunchedBefore = UserDefaults.standard.object(forKey: "lunchedBefore") {
story = UIStoryboard(name: "TShopUI", bundle: nil)
let rootcontroller = story?.instantiateViewController(withIdentifier: "LoginVC")
if let window = self.window {
window.rootViewController = rootcontroller
}
} else {
UserDefaults.standard.set(true, forKey: "lunchedBefore")
story = UIStoryboard(name: "TShopUI", bundle: nil)
let rootcontroller = story?.instantiateViewController(withIdentifier: "MainVC")
if let window = self.window {
window.rootViewController = rootcontroller
}
}
return true
}

关于iOS : getting crash while choosing that see pageviewcontroller for first time that apps loaded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45837531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com