Swift:从自定义单元格中启动相机

时间:2015-01-20 01:59:34

标签: swift ios8

我有一个内置按钮的自定义单元格。当点击该按钮时,我想启动相机。

自定义单元格Xib:

enter image description here

//自定义按钮单元格类 代码:

import UIKit

class CameraCell: UITableViewCell, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    @IBOutlet weak var buttonCamera: UIButton!

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    @IBAction func buttonCameraTapped(sender: UIButton) {

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {

    let imagePicker = UIImagePickerController()

    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
    imagePicker.mediaTypes = [kUTTypeImage]
    imagePicker.allowsEditing = false

    self.presentViewController(imagePicker, animated: true,
    completion: nil)
    //This 'CameraCell' does not have a member named 'presentViewController'
        }
    }

错误:This 'CameraCell' does not have a member named 'presentViewController'

我还需要一个委托(UIImagePickerControllerDelegate)来处理拍摄照片后的事件。

如何从自定义单元格内的按钮启动相机并将其设置为Camera Delegate?

1 个答案:

答案 0 :(得分:2)

因为您位于UITableViewCell的子类中,所以如何调用presentViewController。只有UIViewController才能调用它。

我要做的是处理您处理单元格的UIImageViewControllerUIViewController的触发器。