سؤال

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