我正在使用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)
我是否包括在openCameraAction
或displayInstruction
功能上面的代碼?
長話短說,您需要在查看層次結構中查看錯誤,即,rootViewController或同一層次結構中的任何UIViewController。 – 2017-02-28 03:17:45
'UIImagePickerController' **是一個'UIViewController',所以你可以試着將'imagePicker'傳遞給'displayInstructions',並且從'self'而不是'self'出現。 –