Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • One Way Binding In Swift

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 631
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: