Skip to content

Instantly share code, notes, and snippets.

@agibson73
Last active May 21, 2016 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agibson73/b8c780e7ffa7364c1e26a2f77d688a80 to your computer and use it in GitHub Desktop.
Save agibson73/b8c780e7ffa7364c1e26a2f77d688a80 to your computer and use it in GitHub Desktop.
//See below class for example usage
class GIBbutton : UIButton{
var action : ((UIButton!)->Void)!
convenience init(frame:CGRect,controlEvents:[UIControlEvents],targetAction:((UIButton!)->Void)!){
self.init()
self.frame = frame
action = targetAction
for event in controlEvents{
self.addTarget(self, action: #selector(GIBbutton.performOurAction), forControlEvents: event)
}
}
func performOurAction(button:UIButton,completion:(UIButton)){
self.action(self)
}
}
import UIKit
class ViewController: UIViewController {
var count = 1
override func viewDidLoad() {
super.viewDidLoad()
// Button 1
let button = GIBbutton(frame: CGRect(x: 60, y: 30, width: 200, height: 60), controlEvents: [.TouchUpInside], targetAction: { button in
button.setTitle("Title \(self.count)", forState: .Normal)
self.count += 1
})
button.backgroundColor = UIColor.orangeColor()
button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
button.setTitle("Title Default", forState: .Normal)
self.view.addSubview(button)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment