View Full Version : How to send a command between views?

Jul 27, 2010, 07:05 PM
I have an app that has a view (lets call it BlueView) that has various buttons on it that when pressed load a modal view that lets you select a date (lets call this view DatePick). I figured out how to pass the date back to the BlueView. The date that was picked will be used in a sub that exists in BlueView called calculate. What I need to figure out now is how do I call the calculate sub from the DatePick view as it is being dismissed? I don't know if i explained this good enough, and I've researched this as best I can. I simply don't know what the command/code that would invoke this is called. I am fairly new to this, but I have been able to work through numerous obstacles so far in my app design. I'm not looking for someone to post code that I can cut and paste, I just need to be lead in the right direction. Any help would be greatly appreciated. Thanks.

Jul 27, 2010, 08:08 PM
First, by "sub" do you mean "method"? Using the proper terminology will help the rest of us to better understand what you're asking.

Jul 28, 2010, 02:02 AM
First, by "sub" do you mean "method"? Using the proper terminology will help the rest of us to better understand what you're asking.

Yeah he means a method, bit of VB terminology there if I'm not mistaken.

Jul 28, 2010, 07:02 AM
You can create a protocol that will allow the DatePick view to communicate with the BlueView.

Here is a link: http://developer.apple.com/mac/library/documentation/cocoa/conceptual/objectivec/Articles/ocProtocols.html#//apple_ref/doc/uid/TP30001163-CH15-TPXREF148

Jul 28, 2010, 03:53 PM
It actually depends on the sub. For instance, chicken breast with Southwestern dressing crashes any app, I am told.

Jul 28, 2010, 06:10 PM
If it's an app wide command, and not unique to a particular pair of objects, sending a Notification from the caller, and registering a notification handler in the callee would also work.

Aug 26, 2010, 05:56 PM
Ok, I've been trying to figure this out and I am missing something. I set up the Protocol in BlueViewController.h like so;

@protocol dateset
[self performSelector:@selector(calculate)];

I added #include "BlueViewController.h" to DatePickController.m and in the selectButtonPressed method I put

@protocol (dateset);

The date definitely gets passed to the BlueView when I press the Select button, but I can't call the calculate method in BlueViewController.m. I know something with the syntax for the protocol is incorrect, but I can't figure out what. I probably didn't describe this perfectly, but by the other posts I hope you can figure out what I am doing. Thanks for helping a newbie.

Aug 26, 2010, 06:07 PM
This sounds like a problem which can be solved by delegation.

Add a delegate instance variable to your DatePick. Have your blueView assign itself as the delegate for your DatePick. Have DatePick check for the existence of a delegate, and, if so, call the delegate method when it is being dismissed (or whatever).