xcode “应用程序试图在 UIImagePicker 中使用 UIAlertController 以模态方式呈现活动控制器”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34070668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:16:55  来源:igfitidea点击:

"Application tried to present modally an active controller" in UIImagePicker with UIAlertController

xcodeswiftuialertcontroller

提问by SagittariusA

In my app I want the user to keep the iphone as horizzontally flat as possibile while he/she takes a picture, so I thought to show an alert when the tilt is too bad:

在我的应用程序中,我希望用户在他/她拍照时尽可能保持 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)
            }
        }
    })
}

Sometimes, either while the user is taking the photo or even at the very beginning, I get this error:

有时,无论是在用户拍照时还是在刚开始时,我都会收到此错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <UIImagePickerController: 0x168ec800>.'

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <UIImagePickerController: 0x168ec800>.'

I have also tried with something simpler like:

我也尝试过一些更简单的东西,比如:

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)
            }
        }
    })
}

but it doesn't work either. I have read many discussions about this in SO and tried other configurations but they don't work. How can I fix it?

但它也不起作用。我在 SO 中阅读了许多关于此的讨论并尝试了其他配置,但它们不起作用。我该如何解决?

回答by Oleg Gordiichuk

Try to replace this line:

尝试替换此行:

presentViewController(imagePicker, animated: true, completion: nil)

With this

有了这个

self.navigationController?.pushViewController(imagePicker, animated: YES)