facebookarchive/KVOController

Selector only executed for .Initial but never again.

Closed this issue · 5 comments

Hi, I am testing to use this project in Swift. In the code below I have an UITextField hooked up as an outlet. I marked the outlet as "dynamic", as suggested here: http://stackoverflow.com/a/25219216, at least that what I think I am suppose to do.

Anyway, when I run the code it prints "New email: test@test.test" since I am setting the text to that (before setting up the KVO), this works since the option .Initial is chosen.

But when I change the text in the UITextField nothing happens. What am I doing wrong?

Is it possible to use this with UITextFields in Swift?

Thanks for any help!

   @IBOutlet weak dynamic var emailField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        emailField.text = "test@test.test"
        setupKVO()
    }

    func setupKVO() {
        let options : NSKeyValueObservingOptions = .New | .Old | .Initial | .Prior
        self.KVOController.observe(self.emailField, keyPath:"text", options: options, action:"newEmail:")
    }

    func newEmail(change: NSDictionary) {
        if let newEmail: String = change["new"] as? String {
            println("New email: \(newEmail)")
        }
    }
grp commented

Have you tried KVO on that property without using KVOController, just the standard NSKeyValueObserving APIs? If not, I'd suggest trying that to help narrow down the problem.

I'm experiencing the same issues. I have tested it with the base KVO apis and it works as expected. Only when I try to use KVOController does the event only fire once on the Initial event.

let options : NSKeyValueObservingOptions = .New | .Old | .Initial | .Prior

// Works. Fires on all updates
user.addObserver(self, forKeyPath: "name", options: options, context: nil)

// Does not work. Fires only on initial value
kvoController.observe(user, keyPath: "name", options: options, block: usernameUpdated)

@Sajjon Your code won't work because UITextField's text property is no KVO compliant, it won't fire KVO events when the property is changed. In fact, most UIKit classes are not KVO compliant at all.
See https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/KVO.html

Note: Although the classes of the UIKit framework generally do not support KVO...

There are some exceptions like UIScrollView's contentOffset is observable, but it's not documented.

Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.

kimon commented

Thank you for your patience. We're going to close out this issue. If you're still experiencing the problem described, please reopen the issue - thanks!