MVVM light - Passing multiple parameters in RelayCommand from XAML - 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.

Related

How does forwardingTargetForSelector: work?

I have a UIBarButtonItem. When it receives a message it cannot handle, I want it to forward that message to a particular view controller.
I thought I might be able to accomplish this using the bar button item's forwardingTargetForSelector: method, but apparently no such property is found on objects of type UIBarButtonItem. (Point of terminology: Does that mean forwardingTargetForSelector: is a private property? edit: Wait, I think I'm confused... methods with a colon at the end aren't properties... so can you ever make public a method (like a getter/setter) to which parameters are passed?)
And does that mean that in order to set the value of forwardingTargetForSelector: I must do it from within the .m file of the object for which I want to set it? Which would mean that I would have to subclass my UIBarButtonItem?
And if so, why is this not a public property of NSObjects?
And moreover, what's the best way to achieve my forwarding goal, preferably avoiding subclassing?
additional information:
It all stems from my inclination to reuse a single action in response to various instances of an identical button being pressed. The action is currently contained in my delegate (see How should I implement [almost] identical actions used throughout various VCs? (Answer: use a category)) and varies only in that it should send a presentViewController message to the view controller that instantiated the button that sent the action. Thus, in the action, I can send a presentViewController message to sender, which is an instance of the button, and I want to be able to forward that message to the view controller that created that instance of the button, which I can do if I set each button's forwarding property immediately after it is instantiated in its respective view controller.
I hoped to avoid the "why" just to make the question shorter, but there ya go.
forwardingTargetForSelector: is not really a property; it's more like a question the runtime asks an instance when the instance doesn't respond to a message.
It can't be a property in the #property/declared-property sense, because each selector could have a different target; there would need to be a mapping behind it. That's just not how declared properties work.
UIBarButtonItem descends from NSObject, and it inherits this method along with all the others, but you can't "set" the forwarding target for a selector from outside an instance (without creating some other machinery to allow you to do so, anyways -- possible, but not available by default).
In order to utilize this method, yes, you have to implement it in the class that is doing the forwarding. This does indeed mean subclassing. It also means that the forwarding instance needs to have a reference to the object to which it is forwarding; this requires careful design.
forwardingTargetForSelector: is all but certainly not the correct way to achieve whatever your goal is. In general, in fact, it's a bit esoteric.
I'm not sure exactly what problem you're trying to solve ("making a button forward messages it doesn't respond to" is still rather general -- in particular, why is that necessary?), so it's hard to be more precise.

Where is implementation of PropertyChanged EventHandler?

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.

global variable that returns a true or false

can anyone show me how to implement a var that displays a true value on user input and this value can be used in other controllers to add a value to a different controller...
i would like to use this code to signal arrays to add objects. If anyone has a better way to implement this I would appreciate that too.
I have an ibaction that requires a uibutton to be clicked.
When this button is clicked i want a variable to be triggered.
add favorites lets say.
When this variable is triggered i want to go to another controller
and say if variable = true add this object to the array.
If you want the changes in a property to trigger things to happen in other objects I think Key-Value Observation is what you need. Or you couild just call a method on your other controller from the action.

How provide own Sent Messages in Interface Builder

I cannot find documents about the way, in which Interface Builder determines the Sent Message outlets for the graphical connections between components triggering events and messages of other components.
I want to generate components encapsulating Finite State Automata. The input part is simple, just define IBAction messages and you can connect them in in Interface Builder. The tricky part is obviously the other end of such connections.
I want to provide for each event triggered by the FSM a distinct outlet, like the 'selector' outlet of a NSButton (listed under 'Sent Messages' on the 'Connections' tab of the inspector).
How do I specify such interfaces programmatically and can I specify more than one of these?
Or is this approach not suitable; would Notifications be a better way? (I am used graphical connections from Visual Age and Parts, so I would prefer them, but in Interface Builder, the support for such connections seems somehow limited).
Thanks in advance
The first part of my question has been ansered in the question 'Send An Action Cocoa - IBAction'. I am still looking for a possibility to define more than one 'Sent Message'.
When you implement your method using IBActions, the object that generated the message (the sender) is passed to the message. So if I have a button on my interface that says "Logout" and an action on some controller object named logout: and I have wired these up, the method receives the instance of the button that triggered it. For example:
- (void)logout:(id)sender
{
// sender is the instance of whichever wired button triggered
// this action. We just NSLog() it for now.
NSLog(#"-[%# logout:%#]", self, sender);
}
Other objects may call this action as well, and may pass themselves as the sender or may pass nil. The details of this would be left up to you as the designer.

How do I populate an NSPopupButton with an array of strings?

Simple question, but I am struggling a little to understand how Interface Builder interacts with the rest of the program developed in X-Code.
My UI has an NSPopupButton that I would like to fill with an array of strings that I provide. I have a controller class that defines the method to execute when the button is clicked. Is that where I would populate the NSPopupButton? It seems like I would need to initialize is somewhere, but I am confused about exactly where to do it. I know I need to use addItemsWithTitles, but where do I call that?
Assuming the list isn't changing over time, awakeFromNib (in the controller) would be a good place to populate the menu. If it's dynamic, then you'd want to populate it when the source of those strings changes.