Don, Your first snippet of code is great and follows MVC just fine. However, your second snippet breaks away from proper MVC, in the vein of over-thinking your architecture.
When in doubt, start simple and extend as needed. In your first snippet, your Controller class is a valid Controller in the MVC sense of things. Just stick it in a NIB file, connect the outlets, and you're all groovy. But here's some things to explicitly avoid: (1) Name classes inappropriately. As mentioned before, "View" should not be a subclass of NSObject without any view components. It should be a subclass of NSView or something similar (NSControl, NSTableView, etc) (2) We Cocoa coders don't usually instantiate views inside -init, but rather inside a NIB file. Having NIBs loaded automatically for us via NSViewController or NSWindowController is pretty standard and good practice. (3) View classes shouldn't usually have Controller code in it. View classes should be generic and reusable, whereas Controller is specific to a single purpose inside an app (or multiple apps, if it's a shared framework). (4) Try to use a prefix in your class names. I usually use SD, like SDView or SDController or SDButton, for instance. This helps prevent namespace collisions. Not entirely relevant to your question, just throwing it out there. -Steven On Fri, Feb 12, 2010 at 4:31 PM, Donald Klett <dskl...@mac.com> wrote: > Once again, I am not understanding some aspect of Objective C and/or Cocoa. > > I created a simple class that contains two NSTextField objects. I used IB > to connect the Controller object with the two text fields. The code > follows. This example runs correctly and does copy the value from one text > field to the other. > > #import <Cocoa/Cocoa.h> > > @interface Controller : NSObject { > IBOutlet NSTextField* textField; > IBOutlet NSTextField* copyField; > } > > - (IBAction) buttonTarget: (id) sender; > > @end > > #import "Controller.h" > > @implementation Controller > > - (IBAction) buttonTarget: (id) sender { > int textValue; > > textValue = [textField intValue]; > [copyField setIntegerValue:textValue]; > } > > @end > > Now if I extend this to two objects (Controller and View), the resulting > code does not execute correctly. Again, I used IB to connect the View > object to the two text fields. Using the debugger I find that the two > NSTextField objects have not been allocated (both have nil values). The > code follows: > > #import <Cocoa/Cocoa.h> > #import "View.h" > > @interface Controller : NSObject { > > View* view; > } > > - (IBAction) buttonTarget: (id) sender; > > @end > > #import "Controller.h" > #import "View.h" > > @implementation Controller > > - (id) init { > if (self = [super init]) { > view = [[View alloc] init]; > } > return self; > } > > - (IBAction) buttonTarget: (id) sender { > [view copyFieldValue]; > } > > @end > > #import <Cocoa/Cocoa.h> > > @interface View : NSObject { > > IBOutlet NSTextField* textField; > IBOutlet NSTextField* copyField; > > } > > - (void) copyFieldValue; > > @end > > #import "View.h" > > > @implementation View > > - (void) copyFieldValue { > [copyField setIntegerValue:[textField intValue]]; > } > > @end > > I have no idea what I am doing wrong. Any help would be most appreciated. > Thanks in advance. > > Don Klett > > _______________________________________________ > > 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/steven.degutis%40gmail.com > > This email sent to steven.degu...@gmail.com > -- Steven Degutis http://www.thoughtfultree.com/ http://www.degutis.org/ _______________________________________________ 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