Hi All,
One Way Binding :- In one way bindings, data flows only from object to UI and not vice-versa. For instance, you can have a UITextField
called as nameTF
which is binded with an object having property nameValue. So when the object value changes, it will be reflected on the iPhone UI, but the UI cannot update the nameValue property in the object.
- First make a UI element and an Object which will hold value for UI element . In this example, i m using
UITextField
@IBOutlet weak var nameTF: UITextField!
dynamic var nameValue = ""
- Now add observer for value changed in nameValue
self.addObserver(self, forKeyPath: "nameValue", options: .New, context: nil)
- Now in same UIViewcontroller , override observeValueForKeyPath method like
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if let anItem = object as? ViewController {
nameTF.text = anItem.nameValue
print(nameTF.text!)
}
}
Now whenever value changed in nameValue variable then it will reflect to your UITextfield automatically.
0 Comment(s)