使用 UIAlertController 在 UIImagePicker 中“应用程序试图以模态方式呈现活动控制器”
Posted
技术标签:
【中文标题】使用 UIAlertController 在 UIImagePicker 中“应用程序试图以模态方式呈现活动控制器”【英文标题】:"Application tried to present modally an active controller" in UIImagePicker with UIAlertController 【发布时间】:2015-12-03 16:04:02 【问题描述】:在我的应用程序中,我希望用户在拍照时尽可能保持 iphone 水平平坦,因此我想在倾斜度太差时显示警报:
@IBAction func takeSnapshot(sender: UIButton)
self.imagePicker = UIImagePickerController()
self.imagePicker.delegate = self
self.imagePicker.sourceType = .Camera
self.startMotionManager()
presentViewController(imagePicker, animated: true, completion: nil)
func startMotionManager()
let alertController = UIAlertController(title: "Attenzione!", message: "Mantieni l'iphone il più orizzontale possibile!", preferredStyle: .Alert)
var showAlert:Bool = false
self.motionManager.deviceMotionUpdateInterval = 0.01
self.motionManager.startDeviceMotionUpdatesUsingReferenceFrame(CMAttitudeReferenceFrame.XArbitraryZVertical, toQueue: NSOperationQueue.currentQueue()!, withHandler:
deviceManager, error in
print(deviceManager?.gravity)
/* se inclino troppo o troppo poco l'iphone */
if ((deviceManager?.gravity.z >= -0.999) && (deviceManager?.gravity.z <= -0.980))
if ((!alertController.isViewLoaded()) || (alertController.view.window == nil))
self.imagePicker.presentViewController(alertController, animated: true, completion: nil)
/* se l'iphone è inclinato bene allora nascondo l'alert */
else
if ((alertController.view.window != nil) || (alertController.isViewLoaded()))
alertController.dismissViewControllerAnimated(true, completion: nil)
)
有时,无论是在用户拍照时,还是在刚开始时,我都会收到此错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <UIImagePickerController: 0x168ec800>.'
我也尝试过一些更简单的方法,例如:
func startMotionManager()
let alertController = UIAlertController(title: "Attenzione!", message: "Mantieni l'iphone il più orizzontale possibile!", preferredStyle: .Alert)
var showAlert:Bool = false
self.motionManager.deviceMotionUpdateInterval = 0.01
self.motionManager.startDeviceMotionUpdatesUsingReferenceFrame(CMAttitudeReferenceFrame.XArbitraryZVertical, toQueue: NSOperationQueue.currentQueue()!, withHandler:
deviceManager, error in
print(deviceManager?.gravity)
/* se inclino troppo o troppo poco l'iphone */
if ((deviceManager?.gravity.z >= -0.999) && (deviceManager?.gravity.z <= -0.980))
if (!showAlert:Bool)
showAlert = true
self.imagePicker.presentViewController(alertController, animated: true, completion: nil)
/* se l'iphone è inclinato bene allora nascondo l'alert */
else
if (showAlert)
showAlert = false
alertController.dismissViewControllerAnimated(true, completion: nil)
)
但它也不起作用。我在 SO 中阅读了许多关于此的讨论并尝试了其他配置,但它们不起作用。 我该如何解决?
【问题讨论】:
【参考方案1】:尝试替换这一行:
presentViewController(imagePicker, animated: true, completion: nil)
有了这个
self.navigationController?.pushViewController(imagePicker, animated: YES)
【讨论】:
感谢您的回复,但恐怕不行。现在图像选择器将不再启动...以上是关于使用 UIAlertController 在 UIImagePicker 中“应用程序试图以模态方式呈现活动控制器”的主要内容,如果未能解决你的问题,请参考以下文章
当 UIAlertController 在按钮按下时被关闭时收到通知
从 UINavigationItem 扩展中呈现 UIAlertController