swift:Class-Extraction之后未调用UIPickerViewController委托 [英] swift : UIPickerViewController delegates not called after Class-Extraction
问题描述
我正在开发一个应用程序,该应用程序要求用户从不同的ViewController拍摄自选图像的自拍照.我只想为此提取类.但是,在提取类之后,不会调用 UIPickerViewController
的 delegate
方法.
I am working on an app which requires user to take selfie of pick Images from different ViewController. I want to extract class just for this purpose. But after extraction of class the delegate
methods of UIPickerViewController
are not being called.
现在,我在MultiMediaManager.swift文件中有一个名为 MultiMediaManager
的类.
right now, I have a class called MultiMediaManager
inside MultiMediaManager.swift file.
class MultiMediaManager: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
let imagePicker = UIImagePickerController()
var viewController: UIViewController?
var delegate: MultiMediaManagerDelegate?
init(presentingViewController: UIViewController) {
viewController = presentingViewController
super.init()
imagePicker.delegate = self
}
func showPictureSourceOptions() {
var presentationStyle: UIAlertControllerStyle = .ActionSheet
let galleryAction = UIAlertAction(title: "Photo Library", style: UIAlertActionStyle.Default) { (action) in
self.showPhotoGallery()
}
alertController.addAction(galleryAction)
self.viewController?.presentViewController(alertController, animated: true, completion: nil)
}
private func showPhotoGallery() {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
viewController?.presentViewController(imagePicker, animated: true, completion: nil)
// every thing is working till here as expected.
// note: here, debugger prints MultiMediaManager as imagePicker.delegate
}
显示了从图库中选择图片的视图.但是,当我选择图像时,视图控制器只是默默关闭,并且不会调用其委托.
The View to pick and choose the picture from gallery is presented. But When I choose image, the viewcontroller just dissmiss silently and its delegate is not called.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// break point does not stop here. nither print statement work.
viewController?.dismissViewControllerAnimated(true, completion: nil)
}
我整天都在挠头,但是我仍然不知道发生了什么.
I have been scratching my head the whole day, But I still don't have any idea what's going on.
推荐答案
这些代码没有错.问题是它的调用方式.
There was nothin wrong with these code. The problem was the way it was called.
func showPictureSourceOptions() {
var mediaManager = MultiMediaManager(presentingViewController: self)
mediaManager!.showPictureSourceOptions()
}
每次调用该函数时,我都会创建一个新的 mediaManager
局部变量.删除 var
在 mediaManager
前面解决了该问题.
I was creating new local variable of mediaManager
every time the function was called. removing the var
In front of the mediaManager
fixed the problem.
这篇关于swift:Class-Extraction之后未调用UIPickerViewController委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!