A Closure To Define Buttons
In an App I'm developing right now to be launched with iOS8, there are two buttons. Both buttons look the same except for the title. One way to deal with this and still conforming to the [DRY principle](http://en.wikipedia.org/wiki/Don%27t_repeat_yourself) is this:
let makeButton = { (title: String) -> UIButton in
let button = UIButton.buttonWithType(.System) as UIButton
button.setTranslatesAutoresizingMaskIntoConstraints(false)
button.layer.cornerRadius = 40
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.yellowColor().CGColor
button.setTitle(title, forState: .Normal)
return button
}
workButton = makeButton(NSLocalizedString("Work", comment: "Start working button title"))
breakButton = makeButton(NSLocalizedString("Break", comment: "Start break button title"))
If I now have to change the border width (which I had to do already to refine the design) I only have to change it once. And it is still at the place where it belongs to, the init method of the super view.
I've also put that in a code snippet because most of my projects include at least one button.
If you have a better/different solution for this, let me know on twitter and App.net.