Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 窗口出现时未设置标签上的文本_Objective C_Cocoa_Nsview_Nswindow_Nswindowcontroller - Fatal编程技术网

Objective c 窗口出现时未设置标签上的文本

Objective c 窗口出现时未设置标签上的文本,objective-c,cocoa,nsview,nswindow,nswindowcontroller,Objective C,Cocoa,Nsview,Nswindow,Nswindowcontroller,我用一个自定义的MyWindow.xib和一个MyWindowController创建了一个小型Cocoa应用程序。该窗口包含一个NSTextField(标签),我正在从我的AppDelegate初始化此控制器及其视图,在标签上设置文本并显示窗口: self.controller =[[MyWindowController alloc] initWithWindowNibName:@"MyWindow"]; [self.controller setText:@"My text"]; [self.

我用一个自定义的MyWindow.xib和一个MyWindowController创建了一个小型Cocoa应用程序。该窗口包含一个NSTextField(标签),我正在从我的AppDelegate初始化此控制器及其视图,在标签上设置文本并显示窗口:

self.controller =[[MyWindowController alloc] initWithWindowNibName:@"MyWindow"];
[self.controller setText:@"My text"];
[self.controller showWindow:self];
但当窗口出现时,默认的“标签”文本仍会显示。为什么?


我的示例项目可以在这里下载:。

这里的问题是,您的标签实际上还不可见,因此更改文本没有任何作用。解决此问题的一种方法是在控制器中设置string属性并设置:

self.controller.textForLabel = @"My text";
然后在
视图中将出现:
在控制器的.m中,您可以设置文本:

[self setText:self.textForLabel];

在项目中,在显示窗口之前更改文本,这是在初始化之前完成的。只需切换此选项:

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

[self.controller showWindow:self];
[self.controller setText:@"My text"];
有两种解决方法: 替换两行代码,在显示窗口后设置文本,使其看起来像:

[self.controller showWindow:self];
[self.controller setText:@"My text"];
另一种方法是在“-(void)windowDidLoad”函数中更改MyWindowController.m文件中的标签文本