2017-02-28 24 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

你的受歡迎的人。 –

相關問題