Skip to content

Instantly share code, notes, and snippets.

@tomkowz
Created November 10, 2014 14:16
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 tomkowz/b7ba2994b44de589a41c to your computer and use it in GitHub Desktop.
Save tomkowz/b7ba2994b44de589a41c to your computer and use it in GitHub Desktop.
class MessageControl: UIView {
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var actionButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
self.actionButton.enabled = false
}
override func awakeAfterUsingCoder(aDecoder: NSCoder) -> AnyObject? {
if self.subviews.count == 0 {
let nib = UINib(nibName: "MessageControl", bundle: nil)
let view = nib.instantiateWithOwner(nil, options: nil).first! as MessageControl
view.setTranslatesAutoresizingMaskIntoConstraints(false)
return view
}
return self
}
@IBAction func onActionPressed(sender: AnyObject) {
let success = self.sendMessage(self.textField.text)
let message = success ? "Message sent" : "Message not send"
UIAlertView(title: nil, message: message, delegate: nil, cancelButtonTitle: "Ok").show()
if (success) {
self.textField.text = ""
self.validateActionButton()
}
}
private func validateActionButton() {
self.actionButton.enabled = countElements(self.textField.text) > 0
}
private func sendMessage(message: String) -> Bool {
/// some logic here ...
return true
}
@IBAction func textFieldDidChange(sender: AnyObject) {
self.validateActionButton()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment