Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//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