Where is implementation of PropertyChanged EventHandler? - inotifypropertychanged

Im a bit confused. I dont understand what code is actually is executed when I implement INotifyPropertyChanged interface.
As i imagine the chain goes like this:
My class impliments
INotifyPropertyChanged=>
Every property`s setter calls
NotifyPropertyChanged method=>
PropertyChangedEventHandler
invokes=>???
And i wonder what code makes my control rerender.
Thanks.

The control will subscribe to the event when it binds. When you raise the event, the control will check whether the property that's been changed is one of the ones it cares about. If it is, it will fetch the new value of the property, and rerender itself.
Of course, the handler doesn't have to be to do with controls rerendering - they can do anything. It's just a way of saying, "Hey, property X has changed its value... if you care about that, do something." You can add your own handlers very easily, just like any other event handlers.

Related

Changing a property right after removing observers still sends out a KVO Notification

I have some scenarios where a property is changing, but I do not want to notify observers of the change.
Here is what the code looks like:
[self.fontColorWell removeObserver:self.toolController forKeyPath:#"color"];
[self.fontColorWell setColor:[self.toolController valueForKey:[self fontColorKeyPath]]];
Unfortunately, when the second line executes, the observer (self.toolController) on self.fontColorWell still gets notified.
I'm pretty sure I know why this is happening, because KVO works by the runtime subclassing the observed class, and overridding the observed property to add didChange and willChange messages. (https://www.mikeash.com/pyblog/friday-qa-2009-01-23.html)
If I set a breakpoint at the second line, I can see clearly the class type of self.fontColorWell: NSKVONotifying_NSColorWell. So when -setColor is called, it's still called on the runtime subclass, which has the overridden setter that notifies observers.
But I figured that because I called removeObserver right before, shouldn't the observer be removed from the KVO lookup table, and thus even though the KVO object notifies observers, my self.toolController shouldn't be notified?
I know 100% that when -setColor is called, self.toolController immediately receives the -observeValueForKeyPath... message. I also am positive that all of there are no extra instances of my objects hanging around
Am I just not allowed to do what I'm trying to do? Or am I going about this the wrong way?

How to use the XCode documentation

I'd like to listen in to the text change event for an NSTextField, so I looked at the docs and saw a textDidChange event. So, I connect my text field to a delegate and implement the method, run the app, and nothing happens. I also tried textDidEndEditing, but to no avail.
What are these methods for, exactly? How can one trigger them? Changing the text and tabbing out of the field or pressing enter doesn't do anything. After a bit of googling I found a controlTextDidChange method (in the NSControl parent class of NSTextField) so I implemented it, and it worked (though it had to be an override func, not just a plain func).
Handling the text change event in .NET would be a cinch, just switch to the events panel and double click on "changed" and lo and behold, it creates a method stub into which I can handle the event.
Obviously, I'm an XCode newb comparatively. What's the typical way to go about handling events in XCode/Swift? Did I go about it the wrong way, and is there a better/easier way?
Thanks.

Observer pattern - observer creation

I'm learning about design patterns and I have stumbled upon a question I really don't know how to find an answer to. In the observer design pattern class diagrams, I have seen that a Concrete Observer usually has a reference to the Subject. But, who sets the value of that reference? And how Attaching function gets called? Do observers call it themselves according to the subject reference they have, or somebody else sets the subject and then attaches the observer to the subject? I've looked for examples, but I'm still having troubles finding the best way to implement this.
The observer is the component that wants to be notified about changes or events of the subject. It decides to observe the subject and adds itself to the list of observers that the subject maintains.
The typical use-case is a graphical panel containing a button. The graphical panel creates a button and adds it to itself. And it wants to display a dialog box every time the button is clicked. So it adds itself as an observer of the button, and the button notifies the panel when it's clicked.
In this example, the observer creates the object it observes. But there are situations where that is not the case, and when the reference to the subject is passed as an argument to its constructor or one of its methods. This is irrelevant to the principle of the observer pattern itself.
The Subject is an object which controls some event or has some property which the Observers are interested in. The Observers register themselves with the Subject to express that interest, and the Subject keeps a list of those registered Observers.
When the Subject's property changes, or the event of interest occurs, the Subject iterates through its list of registered Observers and notifies them of the change or event.
The specifics of how the Observers are notified can vary. It may be that they have a well known method that gets called. It may be that they specify a custom method that they want called, which they specify as part of the registration process.

What's the perferred event to handle the end of user interaction with a UIControl?

I have a view with multiple dynamically created UITextfields and UISegmented controls on it (but for purposes of this question, there could also be UIButtons, UISwitches, UISliders, or anything else that inherits from UIControl). I want to preform an action whenever the user finished interacting with any of the controls, regardless of what subclass of control it belongs to. From looking at other questions, I think I want to use addTarget:action:forControlEvents: to add observers to each of my controls after they are created, but I don't know which event I'm looking for. I've tried all the ones that are listed in the Apple Docs here that seemed relevant but none of them seem to be triggered everytime. I'm looking for something like .LostFocus in VBA, but I can't seem to find out what that is - I know there is a becomeFirstResponder method to make a control active, but I can't find anything like a "lostFirstResponder" event.
I suppose I could use isKindOfClass to tell what kind of control it is, and set up my event accordingly, but that seems a little sloppy and I feel like there should be a more direct way to do it. I could also probably set up a UITapGestureRecognizer and build up something that way, but that still feels like a workaround and not really the way it's supposed to be done.
If you're willing to subclass, you can override -resignFirstResponder to detect lost "focus", and act accordingly. This is probably only useful for things like textfields which can hold first responder status, and would not work for UISwitch for instance.
Since all UIControl objects are just UIViews, you can also override touchesEnded to detect the end of interaction with these elements.. although the more accepted way is to add your dismissal handler method as an action for all the UIControlEvents that indicate end of interaction, or just UIControlEventValueChanged.
More info on UIResponder here from Apple's Documentation:
https://developer.apple.com/library/ios/documentation/uikit/reference/UIResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/UIResponder/resignFirstResponder
Many UIKit classes have delegate methods that indicate when interactions have ended, for instance UITextField has a textFieldDidEndEditing method. UITextView has similar methods.

MVVM light - Passing multiple parameters in RelayCommand from XAML

I have more than one PasswordBox on my view and I want to pass all their SecureStrings to my view model when I click a button.
My guess is that I want to populate an instance of a custom class with all the SecureStrings and pass that object as a parameter to the RelayCommand bound to the button.
If I only knew how...
My current idea for a work around:
In the RelayCommands action for the button: send out a NotificationMessageAction with a callback taking a custom class as parameter.
Register for that message in the views code behind, and then populate an object with the SecureStrings, and then pass that object back to the view model with the help of the callback. Not very nice...
There must be a better way to do this in XAML, right?
Actually, I think what you want to do is implement event handlers, or an attached behavior on your PaswordBoxes that will push the SecureStrings to properties in the same viewmodel object that will be handling the RelayCommand's action. Then your RelayCommand won't need any parameters at all.