Created
November 10, 2014 14:16
-
-
Save tomkowz/b7ba2994b44de589a41c to your computer and use it in GitHub Desktop.
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
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