ios - 从 UIImagePickerController() 选取图像后,我尝试显示 UIAlertViewController() 但它没有出现并锁定屏幕

标签 ios swift

当用户从相册中选择图像后,系统会提示他们在 UIAlertController 中输入一个值。但是, Controller 不会出现,并且屏幕变得无法点击。

这很奇怪,因为我在用户拍照时使用完全相同的代码。

当 self.getVal() 方法未被调用时,代码可以工作。它按预期工作。通过日志记录,代码中没有错误。基本上,这就像在当前 viewController View 后面创建了 AlertController。屏幕变得完全无法点击,因为它没有被关闭。 (这是我的两便士)

参见下面的代码:

@IBAction func addBarButtonPressed(sender: UIBarButtonItem) {

    var alert = UIAlertController(title: "Camera or Existing Photo", message: "Take a photo with the camera, or use an existing photo on your device.", preferredStyle: UIAlertControllerStyle.Alert)

    alert.addAction(UIAlertAction(title: "Camera", style: .Default, handler: { (action: UIAlertAction!) in
        self.presentCamera()
    }))

    alert.addAction(UIAlertAction(title: "Photos", style: .Default, handler: { (action: UIAlertAction!) in
        self.presentPhotos()
    }))

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

}

func presentPhotos(){
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){

        let imagePickerController = UIImagePickerController()
        imagePickerController.delegate = self
        imagePickerController.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
        imagePickerController.mediaTypes = [kUTTypeImage as String]
        imagePickerController.allowsEditing = false

        self.presentViewController(imagePickerController, animated: true, completion: nil)
    }
}

func presentCamera() {

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){

        let imagePickerController = UIImagePickerController()
        imagePickerController.delegate = self
        imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
        imagePickerController.mediaTypes = [kUTTypeImage as String]
        imagePickerController.allowsEditing = false

        self.presentViewController(imagePickerController, animated: true, completion: nil)
    }
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

    self.dismissViewControllerAnimated(true, completion: nil)

    if (picker.sourceType == UIImagePickerControllerSourceType.Camera || picker.sourceType == UIImagePickerControllerSourceType.SavedPhotosAlbum)
    {
        let myImageName = NSUUID().UUIDString
        let imagePath = ImageHelperService.fileInDocumentsDirectory(myImageName)

        if ImageHelperService.saveImage(image, path: imagePath) {

            // TODO:
            // Error: This right here, when removed it runs and dismisses and updates fine, just the alertcontroller never appears so can never be dismissed
            self.getVal()

            while(!self.alertViewDismissed) {
                NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate(timeIntervalSinceNow: 0.1))

            }

            let success = DataOperationsService().SaveThingyMaBob(myImageName, value: self.alertViewValue!, bar: self.bar!)            
            if success {

                self.tableView.reloadData()
                self.doChart()

            } else {
                // TODO: Handle error
                debugPrint("Error, save failed")
            }

        } else {
            print("image save failed")
        }        
    }
}

func getVal() {
    self.alertViewDismissed = false

    let alert = UIAlertController(title: "Enter a value", message: "val", preferredStyle: .Alert)

    alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
        textField.text = ""
        textField.keyboardType = .DecimalPad
    })

    alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action) -> Void in
        print("Skip")
        self.alertViewValue = nil
        self.alertViewDismissed = true
    }))

    alert.addAction(UIAlertAction(title: "Submit", style: .Default , handler: { (action) -> Void in
        let textField = alert.textFields![0] as UITextField
        print("Text field: \(textField.text)")
        self.alertViewValue = UtilitiesService.textFieldUnwrapToDouble(textField.text!)
        self.alertViewDismissed = true

    }))

    self.presentViewController(alert, animated: true, completion: nil)
}

最佳答案

我通过在选择图像之前调用 UIAlertController 解决了这个问题。

这不是一个确切的解决方案,但如果您可以灵活地选择首先显示的内容,那么这更像是一种解决方法。

关于ios - 从 UIImagePickerController() 选取图像后,我尝试显示 UIAlertViewController() 但它没有出现并锁定屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36625866/

相关文章:

ios - 填充占位符文本 ios

ios - iOS swift 3 中的回调

ios - 如何从 Cocoa Touch 框架访问窗口或 Root View Controller ?

ios - 边界碰撞检测

ios - 我想在快速代码中访问第二个 View Controller 中的 var

ios - 如何通过依赖注入(inject)使用属性初始化编程 UITableViewCell?

ios - 按钮在 ScrollView 上保持 float

ios - Firebase .childAdded 和事件指示器

ios - 当场景嵌入 NavigationController 时,如何防止 UIScrollView 被推下?

ios - 当我改变它的坐标时,自定义 MKAnnotation 消失