Skip to content

Instantly share code, notes, and snippets.

@sferrini
Created March 11, 2016 19:06
Show Gist options
  • Save sferrini/c43ff23878197692a9f9 to your computer and use it in GitHub Desktop.
Save sferrini/c43ff23878197692a9f9 to your computer and use it in GitHub Desktop.
UIButton target/action function must not be private?
//
// SwiftyButtonViewController.swift
// SwiftyButton
//
// Created by Simone Ferrini on 11/03/16.
// Copyright © 2016 Simone Ferrini. All rights reserved.
//
import UIKit
class SwiftyButtonViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(frame: CGRect(x: 50, y: 50, width: 50, height: 50))
button.backgroundColor = UIColor.redColor()
button.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
}
// As far as I can see, the target/action function must not be private,
// otherwise will be thrown an exception like: unrecognized selector sent to instance 0x7f98a04174f0
// I guess because the "button" instance doesn't have access to the function.
// In Objective-C, visibility is controlled by the headers, but in Swift how we hide this function from the other classes?
private func buttonPressed(sender: UIButton) {
print("buttonPressed")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment