• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Connected Objects being allocated
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Connected Objects being allocated


  • Subject: Re: Connected Objects being allocated
  • From: "Henry McGilton (Boulevardier)" <email@hidden>
  • Date: Fri, 12 Feb 2010 14:28:13 -0800

On Feb 12, 2010, at 1:31 PM, Donald Klett 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

Okay, all is groovy so far.    What you've left unstated in the explanation above is that the
two text fields must have been allocated *somewhere*.    I can make a SWAG that you
dragged a couple of text fields from IB's palette onto the (content view) of the main window
that shows up when you open MainMenu.xib, yes ?

If that is the case, you have two (instantiated and correctly * initialised by IB) text fields that
you can connect to from your (presumably also instantiated and initialised by IB) Controller
object.


> 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"
>

Minor detail here.    Use @class forward references in .h files instead
of importing the class's .h file in your Controller's .h file.    In other words,
delete the    #import "View.h   line and add a   @class  View;    line . . .

This tells the compiler you'll be referencing the View class in your .h file
with an unspoken promise you will include the  View.h  file in your Controller's  .m
file . . .

> @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;
> }

Below, you have  View  as a sub-class of NSObject.    Why?
If View is supposed to be a sub-class of NSView, your [[View alloc] init]
above needs to be [[View alloc] initWithFrame: . . .]
>
> - (IBAction) buttonTarget: (id) sender {
> 	[view copyFieldValue];
> }
>
> @end
>
> #import <Cocoa/Cocoa.h>
>
> @interface View : NSObject {
>
> 	IBOutlet NSTextField*	textField;
> 	IBOutlet NSTextField*	copyField;
>
> }
>
> - (void) copyFieldValue;
>
> @end

Based on this declaration, do you have a View object instantiated in your MainMenu.xib,
and did you then connect the two outlets to the two text fields from that already instantiated object?
If so, after then instantiating a View object in your Controller, your Controller is now talking to
the wrong View object and the one from the xib is being ignored . . .

>
> #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.

I hope this helps a little.     One of the more common beginner mistakes
is to instantiate an object in the xib, and then instantiate a completely
new version in code . . .

And as (I think) someone already pointed out, if you have a class whose
name is View, anybody reading your code superficially would expect it to
*be* something that relates to existing NSView or UIView classes and have
View-like behaviour . . .

A useful exercise to do is to look at your xib objects and draw a picture of
the objects and their connections --- that essentially defines your 'Application
Architecture', and a picture can quickly show up anomalies in the object
graph --- either orphan objects as I suspect in the case, or objects that aren't
really doing anything useful . . .

    Cheers,
         . . . . . . . .    Henry



=================================================
iPhone App Development and Developer Education . . .
        Visit  www.nonatomic-retain.com

Mac OSX Application Development, Plus a Great Deal More . . .
        Visit  www.trilithon.com

_______________________________________________

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

References: 
 >Connected Objects being allocated (From: Donald Klett <email@hidden>)

  • Prev by Date: Re: get the list of controls in a NSView
  • Next by Date: Re: Connected Objects being allocated
  • Previous by thread: Connected Objects being allocated
  • Next by thread: Re: Connected Objects being allocated
  • Index(es):
    • Date
    • Thread