ios - Swift - 有没有办法在相机处于事件状态时呈现 AlertController?

标签 ios swift swift3 uiimagepickercontroller

我使用的是 Swift 3、Xcode 8.2。

我目前有一个应用程序,让用户打开相机拍照。打开相机后,我希望出现一个警告框,为用户提供一些信息。在控制权再次返回到相机之前,用户可以单击“完成”。

这是我的代码:

func openCameraAction() {


    // Check if the camera is available on the device as a source type
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {

        // declare a variable of image picker controller
        let imagePicker = UIImagePickerController()

        // set its delegate, set its source type
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera

        // tell our image picker to not edit a captured picture
        // which means that we won’t get the black screen with
        // a square frame right after taking a photo.
        imagePicker.allowsEditing = false

        // present the camera controller, it will show up from the
        // bottom of the screen, which is the default iOS animation
        // for opening new screens.
        self.present(imagePicker, animated: true, completion: nil)


    }
}

func displayInstructions() {

    let instructionController = UIAlertController(title: "Photo Instructions", message: "Some instruction here", preferredStyle: .alert)

    let actionDone = UIAlertAction(title: "Done", style: .cancel) { (action:UIAlertAction) in
        //This is called when the user presses the done button.
        print("You've pressed the done button");
    }


    //Add the buttons
    instructionController.addAction(actionDone)

    //Present the instruction controller
    self.present(instructionController, animated: true, completion: nil)
}

我尝试设置 completion领域 openCameraAction调用方法displayInstruction但很快意识到我不允许以嵌套的方式呈现事物。我相信呈现必须从 Root View Controller 级别发生,对吗?

Attempt to present ... on ... whose view is not in the window hierarchy!

但是,有什么办法可以达到这个效果吗?做一些嵌套的礼物吗?

感谢您的帮助。

编辑

我看了另一个问题,还是有点困惑。我从其他答案中抓取了一段代码,但很难弄清楚如何集成到我的代码中。

        var rootViewController = UIApplication.shared.keyWindow?.rootViewController
        if let navigationController = rootViewController as? UINavigationController {
            rootViewController = navigationController.viewControllers.first
        }
        if let tabBarController = rootViewController as? UITabBarController {
            rootViewController = tabBarController.selectedViewController
        }
        rootViewController?.present(alertController, animated: true, completion: nil)

我是否将上述代码包含在 openCameraAction 中或displayInstruction功能?

最佳答案

您应该必须在 UIImagePickerController 上呈现 UIAlertController。 我已经修改了你的代码。请参阅下面的代码。

func openCameraAction() 
{
    // Check if the camera is available on the device as a source type
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) 
    {

        // declare a variable of image picker controller
        let imagePicker = UIImagePickerController()

        // set its delegate, set its source type
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera

        // tell our image picker to not edit a captured picture
        // which means that we won’t get the black screen with
        // a square frame right after taking a photo.
        imagePicker.allowsEditing = false

        // present the camera controller, it will show up from the
        // bottom of the screen, which is the default iOS animation
        // for opening new screens.
        self.present(imagePicker, animated: true, completion: { 

          let instructionController = UIAlertController(title: "Photo Instructions", message: "Some instruction here", preferredStyle: .alert)

          let actionDone = UIAlertAction(title: "Done", style: .cancel)  {(action:UIAlertAction) in
            //This is called when the user presses the done button.
            print("You've pressed the done button");
          }

          //Add the buttons
          instructionController.addAction(actionDone)

          //Present the instruction controller
          imagePicker.present(instructionController, animated: true, completion: nil)

       })
   }
}

我已经尝试过上面的代码。它工作正常。尝试一下!

关于ios - Swift - 有没有办法在相机处于事件状态时呈现 AlertController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42499377/

相关文章:

ios - 在共享扩展中旋转图像

swift - 任何实现 "if let"然后比较(例如字符串比较)的单行代码?

ios - Swift:如何在我的 AVPlayerLayer 顶部添加图像/文本以使其成为视频的一部分?

iphone - 具有嵌套 UINavigationController 的应用程序在 iOS SDK 4.2 中崩溃

ios - 弃用具有多个参数的重命名方法

ios - 没有 USB 的 Xcode 6.1 调试?

ios - Swift3:无法删除部分中的行

ios - App Store 打开的 Xcode 版本比安装的版本旧

ios - 使用 slider 选择 View 背景的颜色

ios - 这是使用嵌入式 cocoa pod 创建动态/嵌入式框架的正确方法吗?