2017-02-28 25 views
1

我正在使用Swift 3,Xcode 8.2。Swift - 在摄像机处于活动状态时是否有办法显示AlertController?

我目前有一个应用程序,我有用户打开相机拍照。一旦相机被打开,我想在那里出现一个警告框,提供给用户一些信息。用户可以再次控制返回到相机之前点击“完成”。

这里是我的代码:

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但很快就意识到,我不允许存在于一个嵌套的方式行事。我相信礼物必须从根视图控制器级别发生,对吗?

尝试呈现...上 ...其视图不在窗口 层次结构!

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

谢谢你的帮助。

编辑

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

 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功能上面的代码?

+0

长话短说,您需要在查看层次结构中查看错误,即,rootViewController或同一层次结构中的任何UIViewController。 – 2017-02-28 03:17:45

+0

'UIImagePickerController' **是一个'UIViewController',所以你可以试着将'imagePicker'传递给'displayInstructions',并且从'self'而不是'self'出现。 –

回答

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) 

     }) 
    } 
} 

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

+0

这就是我需要的。谢谢! – noblerare

+0

你的受欢迎的人。 –

相关问题