"应用程序尝试以模态方式呈现一个活动控制器"在UIImagePicker中使用UIAlertController

时间:2015-12-03 16:04:03

标签: xcode swift uialertcontroller

在我的应用程序中,我希望用户在他/她拍照时保持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 个答案:

答案 0 :(得分:4)

尝试替换此行:

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

有了这个

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