On Dec 24, 2008, at 2:49 PM, Ashley Perrien wrote:

I'm still relatively new to Cocoa (and programming in general) and very new to iPhone but this is more of a general question I've been struggling with.

What is the best / most accepted way to get a message out of an object?

Have you read Apple's guidelines on this subject?
http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/chapter_6_section_1.html

Reading the other chapters of that guide is also recommended.


Scenario 1:
I have a document application with an NSView that is basically a graph of a subset of the data in the document. I'd like to bo able to drag within the graph and have it change what data it's showing. How would I send a message from the view to the document either telling it to update the selection or to request new data? Delegate? Notification? Other?

If the drag operation is only supposed to change what subset of the data is being shown, and is not supposed to actually change the data held by the document, then the view should not be communicating with the document. The selected subset and the way it's being shown fall within the responsibilities on the controller and view side of MVC. The model doesn't need to know that sort of thing, and shouldn't.

The design pattern most often used in this situation is the delegate pattern. The controller will typically be set as the delegate for the view, either in code or by connecting up an outlet in Interface Builder.


Scenario 2:
iPhone app (extremely simple, one view) again with data and a graph of that data. when interacting with the UIView, how do I tell the parent view that something needs changing?

Basically, what's the best way for an ivar object to communicate with it's parent?

An object whose pointer is stored in another object's ivar doesn't have any implicit or automatic relationship with that other object. Talking about "its parent" is just confusing. There's no parent relationship there, except with certain specific types of data structures that are explicitly hierarchical. For example, if you were implementing a tree structure, you might model the parent relationship explicitly. Another example is the view hierarchy, where views may have subviews and a superview. But just an arbitrary object which "has-a" second object as an attribute doesn't establish such a parent relationship.

If you want one object to be able to send a message to another object, you need to arrange for it to have a pointer to the intended receiver. You can do that be explicitly passing it such a pointer, possibly in the init method or just through a setter method.

In general, if you're trying to figure out a good way for custom view objects to interact with controller objects, I recommend that you examine how the preexisting Cocoa view classes do that. It might be something as simple as the target-action mechanism or more involved like the data source of table views.

Regards,
Ken

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to