func openGallery(sender: AnyObject) {
var imagePicker = UIImagePickerController()
imagePicker.delegate = sender
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.allowsEditing = true
sender.navigationController?.presentViewController(imagePicker, animated: true, completion: nil)
}
我想组织我的 UIViewControllers
以将所有图像选择器方法放在一个地方。我想以某种方式编写此方法并使其运行。因为我从多个类访问相机,所以我不想让“发送者”成为特定的 ViewController
类名,这会破坏整个目的。无论我尝试用什么代替“AnyObject”,我都会收到有关 UIImagePickerControllerDelegate
和 UINavigationControllerDelegate
的警告。
我试过这种类型的解决方案也没有用,因为我需要两个代表:
var newSender = sender as UIImagePickerControllerDelegate
我该如何解决这个问题?
最佳答案
您的 Actor 阵容需要的是协议(protocol)构成。
引自“Swift 编程语言”,第 14 页。 545:
You can combine multiple protocols into a single requirement with a protocol composition. Protocol compositions have the form
protocol<SomeProtocol, AnotherProtocol>
. You can list as many protocols within the pair of angle brackets (<>
) as you need, separated by commas.
以下是如何将此概念应用于您的具体案例:
imagePicker.delegate = sender as? protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
关于ios - 如何发送符合 UIImagePickerControllerDelegate、UINavigationControllerDelegate 的 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28475661/