Sam Sam - 1 year ago 118
Swift Question

Error: unrecognized selector sent to instance - Using Camera with OverlayView

I'm having some trouble using the Camera with a custom overlay view.

This is the ViewController I use to set up and present the


import UIKit

class ViewController: UIViewController, CameraOverlayDelegate {

var imagePicker = UIImagePickerController()

@IBAction func pickImageButton(sender: AnyObject) {

if UIImagePickerController.isSourceTypeAvailable(
UIImagePickerControllerSourceType.Camera) {

imagePicker = UIImagePickerController()
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
imagePicker.showsCameraControls = false

let myCameraOverlayViewController = CameraOverlayViewController(
bundle: nil)
let customView = myCameraOverlayViewController.view
customView.frame = imagePicker.view.frame
imagePicker.cameraOverlayView = customView

myCameraOverlayViewController.delegate = self
presentViewController(imagePicker, animated: true, completion: nil)

} else {
print("No camera")

func didTakePhoto(sender: CameraOverlayViewController) {
print("Do something ...")

The custom overlay view is a *.xib-file and this is the CameraOverlayViewController I use:

import UIKit

protocol CameraOverlayDelegate {
func didTakePhoto(sender: CameraOverlayViewController)

class CameraOverlayViewController: UIViewController {

var delegate: CameraOverlayDelegate! = nil

@IBOutlet var myView: UIView!

@IBAction func takePhotoButton(sender: AnyObject) {

When I run the app I get an error (
-[UIView takePictureButton:]: unrecognized selector sent to instance
) as soon as I click the button which should call the

I'm pretty new to Xcode, iOS developing and Swift ... in case the problem is obviously. ;)

Thanks in advance.

Sam Sam
Answer Source

The connection between the UIButton and the @IBAction func takePhotoButton was broken. I fixed it by deleting the whole CameraOverlayViewController and making a new one.

There would have been probably an easier way, but I haven't figured out yet how everything is working together in Xcode.