views:

131

answers:

1

I'm having an issue with creating new windows in Cocoa. Hypothetically speaking, lets say I have "WindowA" and has a button called "myButton". When you click on "myButton", it runs this code in the following class file:

 -(void)openFile2:(id)sender
{
    myNextWindow = [[TestWindowController alloc] initWithWindowNibName:@"MainMenu"];
    NSString *testString = @"foo";

    [myNextWindow showWindow:self];
    [myNextWindow setButtonText:testString];
}

The code in a nutshell makes a duplicate "WindowA" and shows it. As you can see, this code also runs a method called 'setButtonText', which is this:

- (void)setButtonText:(NSString *)passedText
{
    [myButton setTitle:passedText];
}

The problem is that when I call this method locally, in the original window - the button text changes (e.g., [self setButtonText:testString]) it works. However, it does not work in the newly created window (e.g., [myNextWindow setButtonText:testString];)

When I debug the newly created window, step by step, the 'myButton' value it gives is 0x0. Do I have to manually assign controllers/delegates to the new window? I think the 'myButton' in the code isn't associated to the 'myButton' in the newly created window.

How would I fix this problem?

+4  A: 

The first problem is that you are loading the MainMenu NIB/XIB repeatedly. That will do Very Bad Things -- the MainMenu should only be loaded once at application startup.

You want to break out any UI that needs to be loaded repeatedly into a separate NIB/XIB file (the same way a document based application has a MainMenu.xib and Document.xib files).

To properly do this, you need to understand the concept of "File's Owner" and how to leverage it properly. Note that there is also overlap with window controllers and understanding those, if you want to use them, will be helpful.

bbum
OK, just to make sure that I understand you correctly..In my MainMenu NIB, I should only have my main launch code and single-use start-up elements in it, and in another NIB place the GUI objects that I will make duplicates of.Assuming this is correct :PI skimmed that page and noticed that I need to create outlets for the File Owner to the NIB. I've made connections before, but I would prefer to dynamically create windows programatically. I would need to store references to these objects, but how different is it to create these outlets via code instead dragging connections to each other?
Jeffrey Kern
Basically, yes. Main menu for main menu, auxiliary nib files for other stuff. There is almost never a need to create a window programmatically (It is a waste of time a nib file is nothing more than an archive of objects anyway. If you create the UI in code, you are just duplicating what is in the archive via the APIs provided on the various classes.
bbum
@bbumI found this post very helpful in understanding what you meant.http://stackoverflow.com/questions/598082/what-describes-the-files-owner-best-in-objective-c-coca-nib/598218#598218Thank you much for your assistance and pointing me in the right direction.
Jeffrey Kern