【问题标题】:Swift - Is there a way to present an AlertController while the camera is active? [duplicate]Swift - 有没有办法在相机处于活动状态时呈现 AlertController? [复制]
【发布时间】:2017-02-28 02:59:25
【问题描述】:

我正在使用 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)
}

我尝试在openCameraAction 中设置completion 字段来调用displayInstruction 方法,但很快意识到我不能以嵌套的方式呈现事物。我相信礼物必须从根视图控制器级别发生,对吗?

尝试呈现 ... ...谁的视图不在窗口中 等级制度!

但是,有什么方法可以实现这种效果吗?做一些嵌套的礼物吗?

感谢您的帮助。

编辑

我看了另一个问题,仍然有点困惑。我从其他答案之一中获取了一段代码,并且很难弄清楚如何集成到我的代码中。

        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)

我是否将上述代码包含在openCameraActiondisplayInstruction 函数中?

【问题讨论】:

  • 长话短说,您需要在 View Hierarchy 中,如错误所示,即 rootViewController 或同一层次结构中的任何 UIViewController。
  • UIImagePickerController UIViewController,因此您可以尝试将 imagePicker 传递给 displayInstructions 并从 displayInstructions 呈现,而不是从 self 呈现。

标签: ios swift swift3 uiimagepickercontroller


【解决方案1】:

您必须在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)

       })
   }
}

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

【讨论】:

  • 这是我需要的。谢谢!
  • 欢迎您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-21
  • 1970-01-01
  • 2016-04-26
  • 1970-01-01
  • 2017-08-26
  • 1970-01-01
相关资源
最近更新 更多