<分区>
我正在尝试创建一个协议(protocol)来包装使用 UIImagePickerController 的过程,以使其在我的应用程序中更加流线型。我基本上有这样的东西:
public protocol MediaAccessor : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func mediaCaptured(title: String, fileData: NSData, fileType: String)
}
还有一个扩展,它负责请求权限和处理委托(delegate)方法的所有繁重工作:
public extension MediaAccessor where Self : UIViewController {
public func captureMedia() {
//All sorts of checks for picker authorization
let picker = UIImagePickerController()
picker.delegate = self
self.presentViewController(picker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
//implementation of the delegate in extension
//even though everything compiles, this method is not called on picker completion
}
}
所以一切都可以编译,但是通过扩展实现 UIImagePickerControllerDelegate 似乎没有注册。当我显示选择器时,它允许我拍照,但 didFinishPickingImage 调用从未发生。如果我将该调用直接移动到 Controller 中,一切正常,但这样做的想法是将这些东西从 View Controller 中隐藏起来,以获得一个非常干净的界面,从而允许 Controller 从设备访问媒体。通过这样的扩展来实现协议(protocol)方法是行不通的吗?有什么我可以更改以允许它工作,而不必直接在我的 View Controller 中实现协议(protocol)吗?