早上好,我遇到了一个问题,当我进入我的应用程序时,会出现一个带有 func viewDidAppear() 的弹出窗口,但是当我点击“否”按钮时,该按钮会像我们按下主页按钮一样正确退出应用程序。当我重新进入应用程序而不杀死它时,弹出窗口不会重新出现......
我搜索了 3 个小时,但没有找到解决方案...
代码如下:
override func viewDidAppear(_ animated: Bool) {
if warning == true{
self.alertStart()
}
}
func alertStart(){
let alertatstart = UIAlertController(title: "ATTENTION !", message: "La version de cette application est en cours de bêta, de nombreux bug sont à déclarer, souhaitez-vous tout de même continuer ?", preferredStyle: .alert)
alertatstart.addAction(UIAlertAction(title: "Oui", style: .destructive, handler:{ (UIAlertAction) in
self.warning = false
if self.betaKeyAlreadyConfirmed == true {
}else{
self.askDevMail()
}
}))
alertatstart.addAction(UIAlertAction(title: "Non", style: .cancel, handler: { (UIAlertAction) in
self.warning = true
UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
}))
self.present(alertatstart, animated: true)
}
我已经尝试将 func 放入 viewDidLoad() 但它不起作用...
我希望你能帮助我,非常感谢,如果你需要更多信息,请在答案中告诉我
路易斯。
最佳答案
ViewDidAppear
在您按下主页按钮并再次返回时不会被调用。
为 UIApplicationDidBecomeActive
和 UIApplicationWillResignActive
使用通知:
在 ViewDidLoad
中:
NotificationCenter.default.addObserver(self, selector: #selector(self.callBackForActiveNotification), name: Notification.Name.UIApplicationDidBecomeActive, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.callBackForBackgroundNotification), name: Notification.Name.UIApplicationWillResignActive, object: nil)
添加函数:
@objc func callBackForBackgroundNotification(){
// This will be called when your app goes in background
}
@objc func callBackForActiveNotification(){
// This will be called when your app become active, show pop up here again if it is not shown
}
关于ios - 当我们退出应用程序而不终止应用程序 Swift 4 时,弹出窗口不会重新出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50682196/