尝试显示带有消息的 UIAlertController 时应用程序崩溃:由于未捕获的异常而终止应用程序 'NSInvalidArgumentException'
App crashes when attempting to display UIAlertController with message: Terminating app due to uncaught exception 'NSInvalidArgumentException'
此警报控制器已初始化:
let alertVC2 = PMAlertController(title: "Need to always enable location authorization", description: "Go to Settings -> App -> Location. Then select 'Always'.", image: UIImage(named: "Location"), style: .alert)
在 ViewDidLoad()
中,动作被添加到 alertVC2
。
alertVC2.addAction(PMAlertAction(title: "OK", style: .default, action: { () in
print("Capture action OK")
self.alertVC2.dismiss(animated: true, completion: nil)
}))
alertVC2.addTextField { (textField) in
textField?.placeholder = "Location..."
}
此外,在 viewDidLoad()
中添加了这段代码,这将允许我 运行 调用 willResignActive 的函数,当应用程序在后台保持休眠状态后再次激活时:
NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: UIApplication.didBecomeActiveNotification, object: nil)
这是一个函数,当应用再次激活时 运行s:
@objc func willResignActive(_ notification: Notification) {
print("activated")
check()
}
在check()
函数中,将调用alertVC2
(AlertController) 并将警报控制器显示在屏幕上。当我通过将应用程序恢复运行使其恢复生机时,将显示 alertcontroller。但是,当我第二次退出应用程序并再次return时,它不会显示alertVC2
。当我第三次这样做时,应用程序崩溃了。
下面是 check()
函数的简要介绍:
func check() {
if CLLocationManager.authorizationStatus() == .notDetermined || CLLocationManager.authorizationStatus() == .authorizedWhenInUse || CLLocationManager.authorizationStatus() == .denied || CLLocationManager.authorizationStatus() == .restricted {
locationManager.requestWhenInUseAuthorization()
locationManager.requestAlwaysAuthorization()
if CLLocationManager.authorizationStatus() != .authorizedAlways {
print("Need to always authorize location for me")
self.present(alertVC2, animated: true, completion: nil)
}
}
if CLLocationManager.authorizationStatus() == .authorizedAlways {
locationManager.startUpdatingLocation()
}
}
这是我在第三次尝试时应用程序崩溃时收到的错误消息:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: 'Application tried to present
modally an active controller
我该怎么做才能避免崩溃并继续 post alertcontroller?
听起来您正试图多次显示警报控制器。如果在你调用self.present的那一行打断点,它命中了多少次?
如果我猜对了,这里的简单方法是检查视图当前是否正在显示任何内容,如果是,则跳过显示您的警报。也许是这样的:
if CLLocationManager.authorizationStatus() != .authorizedAlways {
print("Need to always authorize location for me")
if self.presentedViewController == nil {
self.present(alertVC2, animated: true, completion: nil)
}
}
或者类似的东西:
if CLLocationManager.authorizationStatus() != .authorizedAlways {
print("Need to always authorize location for me")
if !alertVC2.isBeingPresented { // make sure we're not already presenting this alert to the user
self.present(alertVC2, animated: true, completion: nil)
}
}
此警报控制器已初始化:
let alertVC2 = PMAlertController(title: "Need to always enable location authorization", description: "Go to Settings -> App -> Location. Then select 'Always'.", image: UIImage(named: "Location"), style: .alert)
在 ViewDidLoad()
中,动作被添加到 alertVC2
。
alertVC2.addAction(PMAlertAction(title: "OK", style: .default, action: { () in
print("Capture action OK")
self.alertVC2.dismiss(animated: true, completion: nil)
}))
alertVC2.addTextField { (textField) in
textField?.placeholder = "Location..."
}
此外,在 viewDidLoad()
中添加了这段代码,这将允许我 运行 调用 willResignActive 的函数,当应用程序在后台保持休眠状态后再次激活时:
NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: UIApplication.didBecomeActiveNotification, object: nil)
这是一个函数,当应用再次激活时 运行s:
@objc func willResignActive(_ notification: Notification) {
print("activated")
check()
}
在check()
函数中,将调用alertVC2
(AlertController) 并将警报控制器显示在屏幕上。当我通过将应用程序恢复运行使其恢复生机时,将显示 alertcontroller。但是,当我第二次退出应用程序并再次return时,它不会显示alertVC2
。当我第三次这样做时,应用程序崩溃了。
下面是 check()
函数的简要介绍:
func check() {
if CLLocationManager.authorizationStatus() == .notDetermined || CLLocationManager.authorizationStatus() == .authorizedWhenInUse || CLLocationManager.authorizationStatus() == .denied || CLLocationManager.authorizationStatus() == .restricted {
locationManager.requestWhenInUseAuthorization()
locationManager.requestAlwaysAuthorization()
if CLLocationManager.authorizationStatus() != .authorizedAlways {
print("Need to always authorize location for me")
self.present(alertVC2, animated: true, completion: nil)
}
}
if CLLocationManager.authorizationStatus() == .authorizedAlways {
locationManager.startUpdatingLocation()
}
}
这是我在第三次尝试时应用程序崩溃时收到的错误消息:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller
我该怎么做才能避免崩溃并继续 post alertcontroller?
听起来您正试图多次显示警报控制器。如果在你调用self.present的那一行打断点,它命中了多少次?
如果我猜对了,这里的简单方法是检查视图当前是否正在显示任何内容,如果是,则跳过显示您的警报。也许是这样的:
if CLLocationManager.authorizationStatus() != .authorizedAlways {
print("Need to always authorize location for me")
if self.presentedViewController == nil {
self.present(alertVC2, animated: true, completion: nil)
}
}
或者类似的东西:
if CLLocationManager.authorizationStatus() != .authorizedAlways {
print("Need to always authorize location for me")
if !alertVC2.isBeingPresented { // make sure we're not already presenting this alert to the user
self.present(alertVC2, animated: true, completion: nil)
}
}