Re: A question involving properties
Re: A question involving properties
- Subject: Re: A question involving properties
- From: Karolis Ramanauskas <email@hidden>
- Date: Sun, 11 Oct 2009 16:35:42 -0500
OK, perhaps I am misunderstanding you, but if you are simply trying to set
the label of the NSTextField you may wanna do this. You do not need to
allocate NSTextField programmatically, it will be loaded automatically.
@interface TestAppDelegate : NSObject <NSApplicationDelegate> {
NSTextField * _textField;
}
@property (readonly) IBOutlet NSTextField * _textField;
@end
-----------------------------------------------------------------------
@implementation TestAppDelegate
@synthesize _textField;
- (void)awakeFromNib {
[self._textField setStringValue:@"Hello!"];
}
@end
I assume you have placed a label in your window and connected it to an
IBOutlet in Interface builder. As you can see I set up _textField as a
readonly property because we don't want to change the NSTextField object,
just set its property "stringValue".
Hopefully that's what you meant. Otherwise perhaps someone else will offer a
better solution...
Karolis
On Sun, Oct 11, 2009 at 4:11 PM, Michael de Haan <email@hidden> wrote:
> May I indulge the group.
>
> In doing the Hillegass challenge of Chapter 18 ( creating a doc based app
> to draw ovals), detoured to get a deeper understanding to Apples Sketch-112,
> which in turn lead to properties and ivars, which lead to this little demo
> app to give me some more insight into how properties and ivars worked. The
> problem I have is that I cannot set a label programmatically....and am not
> sure if this is related to my understanding of properties, or my
> understanding of the workings of an NSTExtField.
> ( I did a search of this site, tried, "validateEditing" but still could not
> prevail. )
>
>
> Basically, a non-doc app, with an NSTextField ( whose initial title is
> "label"), an AppController with an IBOutlet ( textField), and with
> AppController's outlet to NSTextField's control connection established in
> the nib.
>
> The relevant code in AppController.h thus.
>
>
>
> @interface AppController : NSObject {
>
> NSTextField * _textField;
>
> }
>
> @property (readwrite, retain) IBOutlet NSTextField *textField;
>
>
> The accessors like this. ( I know that I could have used @synthesize) but I
> wanted to see what was happening in the code.
>
>
> - (NSTextField*) textField
> {
> METHOD_LOG;// << A macro which reveals method's name, and the object
> (self) >>
> NSLog(@"%@", _textField);
> return _textField;
> }
>
> - (void) setTextField: (NSTextField*) t
> {
> METHOD_LOG;
> if ( _textField == t)
> return;
> [_textField release];
> [t retain];
> _textField = t;
> NSLog(@"The value of TextField \"_textField\" is: %@",[_textField
> stringValue]);
>
> // this line a test to see if "validateEditing" works
>
> [_textField validateEditing]; // does not effect outcome
>
> }
>
>
> and finally, in awakeFromNib, this;
>
>
> METHOD_LOG;
> NSColor * g = [ NSColor greenColor];
> NSString *s = @"FooBar";
> NSTextField *f = [[NSTextField alloc]init];
> [f setBackgroundColor:g];
> [f setStringValue:s];
> NSLog(@"The value of TextField \"f\" is: %@",[f stringValue]);
> [self setTextField: f];
> [f release];
>
>
> My output is this: (which includes a warning, which is probably relevant,
> but I have not been able to find much on it);
>
>
>
> MethodName: setTextField: "self" = <AppController: 0x1002140d0>
> The value of TextField "_textField" is: label
> MethodName: awakeFromNib "self" = <AppController: 0x1002140d0>
> The value of TextField "f" is: FooBar
> MethodName: setTextField: "self" = <AppController: 0x1002140d0>
> The value of TextField "_textField" is: FooBar
> __CFServiceControllerBeginPBSLoadForLocalizations timed out while talking
> to pbs
>
>
> So, from my reading of ivars, properties and outlets, it seems that Apple
> now recommends that the properties (not the ivars ) be exposed, which I have
> tried to do. Also, from the output in the console, it seems as if the outlet
> is set, but then reverts back to the word "label". Some insight would be
> appreciated. (It might very well be that this involves something that is yet
> to be covered, and if so, I will move on. But, just wish to know if this is
> something obvious that I have missed).
> Thanks as always.
> _______________________________________________
>
> 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
>
_______________________________________________
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