swift:在Class-Extraction之后没有调用的UIPickerViewController委托

时间:2016-08-15 12:02:11

标签: ios swift delegates uiimagepickercontroller

我正在开发一款应用程序,该应用程序要求用户从不同的ViewController中获取拾取图像的自拍照。我想为此目的提取课程。但是在提取课程后,delegate的{​​{1}}方法没有被调用。

现在,我在MultiMediaManager.swift文件中有一个名为UIPickerViewController的类。

MultiMediaManager

显示从图库中选择图片的视图。但是当我选择图像时,viewcontroller只是默默地解除它并且它的委托没有被调用。

    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 
    }

我整天都在摸不着头脑,但我仍然不知道发生了什么。

1 个答案:

答案 0 :(得分:0)

这些代码没有错。 问题在于它的召唤方式。

func showPictureSourceOptions() {
        var mediaManager = MultiMediaManager(presentingViewController: self)
        mediaManager!.showPictureSourceOptions()
    }

每次调用函数时,我都在创建mediaManager的新局部变量。移除var前面的mediaManager解决了问题。