为什么“picker.delegate = self needed”?

时间:2017-03-01 06:10:11

标签: ios swift delegates

我正在创建一个简单的相机应用程序。

在代码中,UIImagePickerControllerclass是ViewControllerClass实例的委托。

但是为什么在这段代码中需要“picker.delegate = self”?

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBAction func launchCamera(_ sender: UIBarButtonItem) {

        let camera = UIImagePickerControllerSourceType.camera

        if UIImagePickerController.isSourceTypeAvailable(camera) {
            let picker = UIImagePickerController()
            picker.sourceType = camera
            picker.delegate = self
            self.present(picker, animated: true)
    }
}

        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

            let image = info[UIImagePickerControllerOriginalImage] as! UIImage
            self.imageView.image = image
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
            self.dismiss(animated:  true)
        }

1 个答案:

答案 0 :(得分:0)

UIImagePickerController的委托包括UIImagePickerControllerDelegate和UINavigationControllerDelegate。

用于捕获用户的UIImagePickerControllerDelegate'关于拍照选择图片的操作。 UINavigationControllerDelegate用于相册和照片

之间的视图控制器导航