我在这里寻求帮助!
我在 swift 中有一个 ActionController,我试图从创建的一个 Action 中呈现一个 UIImagePickerController,但是当我运行它时,它向我显示了这个警告:尝试呈现在 <...> 上,它已经呈现(null),并且它没有呈现任何东西。
这是我的代码:
let actionController = TweetbotActionController()
actionController.addAction(Action("Photo Library", style: .Default , handler: { action in
let picker = UIImagePickerController()
picker.sourceType = .PhotoLibrary
picker.allowsEditing = false
picker.delegate = self
self.presentViewController(picker, animated: true, completion: nil)
}))
actionController.addAction(Action("Take Photo", style: .Default, handler: {action in
NSLog("Take Photo Pressed")
}))
actionController.addSection(Section())
actionController.addAction(Action("Cancel", style: .Cancel, handler:nil))
presentViewController(actionController, animated: true, completion:nil)
非常感谢!!!
最佳答案
错误信息很容易解释。您不能同时显示两个 View Controller 。
您应该先关闭 AlertController
,然后再打开新的。要实现这一点,您有多种选择,但我相信最简单的方法是在您按下“照片库”并在其中设置某种标志 时关闭 AlertController。这可以是一个名为 shouldOpenPicker
的变量,除非您按下按钮,否则它将始终为 false。
然后您可以使用 presentViewController
(呈现 AlertController 的那个)的completion
闭包来打开选取器 Controller 仅如果标志变量设置为 true
。如果是,则显示选择器 Controller 并再次将标志设置为 false
。
UPDATE:
你应该在关闭第一个 View Controller 时显示第二个 View Controller ,如下所示:
alert.addAction(UIAlertAction(title: "Photo library", style: .Default, handler: { action in
alert.dismissViewControllerAnimated(true, completion: {
let picker = UIImagePickerController()
picker.sourceType = .PhotoLibrary
picker.allowsEditing = false
picker.delegate = self
self.presentViewController(picker, animated: true, completion: nil)
})
}))
关于swift - 警告 : Attempt to present <UIImagePickerController: > on <. ..> 已经呈现(空),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38579679/