ios - 如何发送符合 UIImagePickerControllerDelegate、UINavigationControllerDelegate 的 UIViewController

标签 ios swift uiimagepickercontroller

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”,我都会收到有关 UIImagePickerControllerDelegateUINavigationControllerDelegate 的警告。

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

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/

相关文章:

objective-c - iOS 5 中的异步网络操作

iphone - 自定义 UITextView 占位符在键盘输入时不会删除

ios - 如何在 Swift 中调用 IMP?

ios - 从 UIImagePickerController 调整图像大小不起作用

iphone - 如何以编程方式或强制[无需用户交互]调用 UIImagePickerController Delegate?

ios - 应用程序图标集 "AppIcon"有 8 个未分配的子项?

ios - 具有两个类的按钮上的 swift IBAction

ios - UICollectionViewCell 在 UICollectionViewReusableView 宽度之后启动

ios - 水平自动布局约束

ios - 如何获取通过 iphone 中的照片库选择的图像的名称?