Instantly share code, notes, and snippets.
Last active
May 21, 2016 02:31
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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