gpt4 book ai didi

ios - UIImagePickerController 可以直接在 Story Board 上使用吗?

转载 作者:搜寻专家 更新时间:2023-11-01 06:26:22 24 4
gpt4 key购买 nike

我的印象是 ViewController 的用法是相似的:在 Storyboard上,我们将一个 UIViewController 拖到场景上,然后更改它的类类型,例如到 UIImagePickerController。 (我想做一个专门的取图场景)

但后来我发现如果我直接子类化,UIImagePickerController 将无法工作:

class TestUIImagePickerController: UIImagePickerController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

override func viewDidLoad() {
super.viewDidLoad()
self.sourceType = .photoLibrary
self.delegate = self
// self.present(self, animated: true) // either comment it out or not, both way won't work.
}

但它只有在我将一个 UIViewController 放在 Storyboard上,然后以编程方式构造一个 UIImagePickerController 时才有效:

class SecondTestUIImagePickerController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()

let picker = UIImagePickerController()
picker.sourceType = .photoLibrary
picker.delegate = self
self.present(picker, animated: true)
}

我可以知道我在第一个用法示例中是否遗漏了什么吗?是否必须以编程方式创建 UIImagePickerController 然后通过代理 View Controller (UIVIewController) 呈现它?

最佳答案

self.present(self, animated: true) 

你不能self呈现self,使用另一个ViewController来呈现UIImagePickerController

UIImagePickerController 可以在 Story board 上使用,例如在您的代码中

class SecondTestUIImagePickerController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let sb = UIStoryboard(name: "ImagePickerStoryboard", bundle: nil)
let picker = sb.instantiateViewControllerWithIdentifier("ImagePicker") as! TestUIImagePickerController
self.present(picker, animated: true)
}

请记住 UIImagePickerControllerUINavigationController 的子类

关于ios - UIImagePickerController 可以直接在 Story Board 上使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54000500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com