Re: Accessing IBOutlet From non-IBAction Method
Re: Accessing IBOutlet From non-IBAction Method
- Subject: Re: Accessing IBOutlet From non-IBAction Method
- From: Erik Buck <email@hidden>
- Date: Mon, 13 Feb 2006 20:59:07 -0500
Thank you for posting code.
You need to work through at least one basic Cocoa tutorial before you
try this type of thing. You code and question indicate that you have
failed to grasp some of the basics.
@interface InspectorPanelController : NSObject
{
IBOutlet NSPanel *inspectorPanel;
}
- (IBAction)changeState:(id)sender;
- (void)ipcChangeState;
@end
The IBOutlet is presumably connected to something in Interface
Builder. That suggests that either you created an instance of the
InspectorPanelController class in Interface Builder or an instance of
InspectorPanelController is the file's owner when the nib file is
loaded.
- (void)awakeFromNib
{
NSLog(@"retain count awakefromnib: %d", [inspectorPanel
retainCount]);
NSLog(@"title: %@", [inspectorPanel title]);
}
If awakeFromNib is being called by the frameworks, then you have
indeed created an instance of the InspectorPanelController class in
Interface Builder or an instance of InspectorPanelController is the
file's owner when the nib file is loaded.
- (IBAction)changeState:(id)sender
{
NSLog(@"retain count changestate0: %d", [inspectorPanel
retainCount]);
NSLog(@"inspectorpanel: %@", [inspectorPanel title]);
if ([inspectorPanel isVisible])
[inspectorPanel close];
else
[inspectorPanel orderFront:inspectorPanel];
NSLog(@"retain count changestate1: %d", [inspectorPanel
retainCount]);
}
If you have configured the panel connected to IBOutlet NSPanel
*inspectorPanel to release when closed, the line [inspectorPanel
close]; may result in the panel being deallocated. I suggest that
you either do not configure the panel that way or you call
[inspectorPanel orderOut:nil]; instead of close.
InspectorPanelController *ipc = [InspectorPanelController alloc];
[ipc ipcChangeState];
[ipc release];
Here is where you are completely off the reservation as they say!
First, you allocate an instance of InspectorPanelController but never
initialize it.
Second, I would not expect an IBOutlet connected in Interface Builder
to be connected in the new instance because the new instance is a
completely different instance for the one connected in Interface
Builder.
If there is an instance of InspectorPanelController in the Interface
Builder nib file, you need to use _that_ instance because it is the
only one with a connection. If you intend an instance of
InspectorPanelController to be the file's owner when a nib file is
loaded, you need to write something like the following code:
InspectorPanelController *ipc = [[InspectorPanelController alloc]
init];
[NSBundle loadNibNamed:@"someNibFileName" owner:ipc];
[ipc changeState:nil]; // note: this method has an argument!
[ipc release];
ipc = nil; // This is a good habit
http://www.cocoadev.com/index.pl?FilesOwner
http://developer.apple.com/documentation/Cocoa/Reference/
ApplicationKit/ObjC_classic/Classes/NSBundleAdditions.html
http://developer.apple.com/documentation/Cocoa/Conceptual/
LoadingResources/Concepts/UsingMultiNibFiles.html
http://developer.apple.com/documentation/Cocoa/Reference/
ApplicationKit/ObjC_classic/Protocols/NSNibAwaking.html
http://developer.apple.com/documentation/DeveloperTools/Conceptual/
IBTips/Articles/FreqAskedQuests.html
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden