Re: Connected Objects being allocated
Re: Connected Objects being allocated
- Subject: Re: Connected Objects being allocated
- From: Donald Klett <email@hidden>
- Date: Sat, 13 Feb 2010 10:17:01 -0500
Steven,
Thanks for your analysis. I must admit I am still somewhat confused, but you have given me something to think about. And thanks to Henry McGilton for his reply.
I have David Chisnall's book on Cocoa Programming, so I will try to understand your suggestion concerning using view and window controllers. This is all new. I have been a Java developer for about seven years and want to branch out. So all help is very welcome.
I also have to read more about what happens when an app loads. I thought I had an idea of what happens, but maybe not completely. I am sure I will be back with more questions. Thanks again.
Don
On Feb 12, 2010, at 7:06 PM, Steven Degutis wrote:
> 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 <email@hidden> 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 (email@hidden)
>
> 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:
>
> This email sent to email@hidden
>
>
>
> --
> Steven Degutis
> http://www.thoughtfultree.com/
> http://www.degutis.org/
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden