views:

307

answers:

2

Edit: In case someone wants to look at the actual code, here it is: http://pastie.org/713951

Long story short: the problem I'm having is I can't make the window show up in the fly() function.

Full Description:

I'm creating a plugin for the Mac application 'Coda'. I have a controller 'Bolder', with two outlets:

@class Bolder;

@interface Bolder : NSObject
{
    IBOutlet id MyLabel;
    IBOutlet id theWindow;
}

Coda specifies it's own init method for plugins. In this init method, I am loading a Nib 'Superman' and choosing a method 'fly' to call when my plugin is clicked:

[NSBundle loadNibNamed:@"Superman" owner:self];
[controller registerActionWithTitle:NSLocalizedString(@"OK!", @"Flying Man") target:self selector:@selector(fly:)];

In the 'fly' method, I want to show the window and change the text on a label:

- (void)fly:(id)sender
{
    [theWindow orderFront:self];
    [theWindow makeKeyAndOrderFront:self];
    [MyLabel setStringValue:@"new text"];
}

This last bit is the part that is throwing me – the window just doesn't show up! Yet if I put these same three lines inside 'awakeFromNib' it shows up fine. What's causing this difference? I can't put this code inside awakeFromNib because that causes my plugin's window to show up every time I start Coda.

A: 

Uncheck "Visible At Launch" for the window in Interface Builder if you don't want it to show when you load the nib.

Wevah
Even if I do that, I'm trying to set the text in the label, and I'd like to do it in init...but I can't access it.
Goose Bumper
I don't believe outlets are guaranteed to be hooked up in init; have you tried moving your setters to `-awakeFromNib`?
Wevah
Yes I have...this is for a plugin I'm trying to develop, and I think the problem might be with the app I'm trying to make it for. I've edited the question a bit, take a look if you have the time :)
Goose Bumper
A: 

Try delaying you nib loading until it's time to show the window. E.g.:

- (void)fly:(id)sender
{
    if (!theWindow) 
    {
        [NSBundle loadNibNamed:@"Superman" owner:self];
    }
    else
    {
        [theWindow makeKeyAndOrderFront:self];
    }
}
Darren
I tried that...doesn't make a difference.
Goose Bumper
Edited my answer.
Darren
Thank you! This definitely worked in the sense that I can put all my initializing code in 'awakeFromNib' instead of 'fly' and be okay...but I'm still wondering why I can't access any instance variables or methods from fly. I just created a test instance method and I can call it fine from 'awakeFromNib' but not from 'fly'; can you think of any reason that might happen?
Goose Bumper
What is the value of `self` in fly compared to awakeWithNib?
Darren
How would I get the value? Does Objective-C have a stringValue: function?
Goose Bumper
NSObject has the `description` method. Just use `NSLog(@"Self: %@", self);` or use the debugger.
Darren
Interestingly, they're both the same:Self in awakeFromNib: <Bolder: 0x16307b30>Self in fly: <Bolder: 0x16307b30>
Goose Bumper