質問

I've made a small Cocoa Application with a custom MyWindow.xib and a MyWindowController. The window contains a NSTextField (label) and from my AppDelegate I'm initializing this controller and it's view, setting a text on the label and displays the window:

self.controller =[[MyWindowController alloc] initWithWindowNibName:@"MyWindow"];
[self.controller setText:@"My text"];
[self.controller showWindow:self];

But the default "Label" text is still displayed, when the windows appear. Why?

My sample project can be downloaded here: http://s000.tinyupload.com/?file_id=00611347759347594342.

役に立ちましたか?

解決

The issue here is that your label is not actually visual yet, so changing the text does nothing. One way you can get around this is to have a string property in your controller and set that:

self.controller.textForLabel = @"My text";

Then in your viewWillAppear:in your controller's .m you can set the text:

[self setText:self.textForLabel];

他のヒント

In your project you're changing the text before showing the window, which is done before the initialization. Just switch this:

[self.controller setText:@"My text"];
[self.controller showWindow:self];

to

[self.controller showWindow:self];
[self.controller setText:@"My text"];

Two ways to solve it: Replace two lines of code, set text after showing window, so it should look like:

[self.controller showWindow:self];
[self.controller setText:@"My text"];

Other way is to change your label text in MyWindowController.m file inside of "-(void)windowDidLoad" function.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top