【发布时间】:2019-08-02 07:50:14
【问题描述】:
我有一个入门屏幕,当新用户首次打开应用程序时,我会向他们展示该屏幕。 在我的 appDelegate 中,我检查是否是第一次启动。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var initialViewController = storyboard.instantiateViewController(withIdentifier: "OnBoarding")
let userDefaults = UserDefaults.standard
if userDefaults.bool(forKey: "onBoardingComplete") {
initialViewController = storyboard.instantiateViewController(withIdentifier: "MainApp")
}
window?.rootViewController = initialViewController
window?.makeKeyAndVisible()
}
我还有一个collectionViewCell,我有一些按钮,当我点击它们时,我会收到一个带有信息的警报。
一键示例
@IBAction func guide3Btn(_ sender: Any) {
let infoVC = infoService.info(title: "Title", body: "Information")
self.window?.rootViewController?.present(infoVC, animated: true, completion: nil)
}
当用户第一次启动应用程序时,如果他点击信息按钮会得到这个:
Warning: Attempt to present <MyApp.InfoViewController: 0x7f91db45cfb0> on <MyApp.OnbBoardViewController: 0x7f91db506af0> whose view is not in the window hierarchy!
如果用户重新打开应用程序一切正常。我知道当我们第一次启动时,我们将 onBoarding 作为根控制器,但我不明白如何解决这个问题。
更新
这是 infoService 类。我使用新的故事板来创建警报。
class InfoService {
func info(title: String, body: String) -> InfoViewController {
let storyboard = UIStoryboard(name: "InfoStoryboard", bundle: .main)
let infoVC = storyboard.instantiateViewController(withIdentifier: "InfoVC") as! InfoViewController
infoVC.infoBody = body
infoVC.infoTitle = title
return infoVC
}
}
【问题讨论】:
-
self.window来自哪里? -
我从 collectionViewCell 类中展示它,所以我需要一个 ViewController。
-
我的意思是你如何定义你在函数
guide3Btn中使用的窗口 -
我用 infoService 类更新了我的问题。
标签: swift uialertview