如何发送符合 UIImagePickerControllerDelegate、UINavigationControllerDelegate 的 UIViewController

How to send an UIViewController that conforms UIImagePickerControllerDelegate, UINavigationControllerDelegate

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 以将所有图像选择器方法集中在一个地方。我想以某种方式编写此方法并使其运行。因为我从多个 classes 访问相机,所以我不想让 'sender' 成为特定的 ViewController class 名称,那样会破坏整个目的。无论我尝试用什么代替 'AnyObject',我都会收到有关 UIImagePickerControllerDelegateUINavigationControllerDelegate 的警告。

我试过这种类型的解决方案也没有用,因为我需要两个代表:

var newSender = sender as UIImagePickerControllerDelegate

我该如何解决这个问题?

你的演员表需要的是协议组合

引自“Swift 编程语言”,第 1 页。 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>