NSTextField 不会设置StringValue

发布于 2024-11-24 01:24:38 字数 747 浏览 0 评论 0原文

我刚刚开始开发一个非常简单的应用程序(还没有真正进入 Objective C),其中包含一个包含 NSTextField 的 NSWindow。 appdelegate.h 具有以下内容:

NSWindow *window;
NSTextField *userIDText;

@property (nonatomic, retain) IBOutlet NSWindow *window;
@property (nonatomic, retain) IBOutlet NSButton *nextButton;
@property (nonatomic, retain) IBOutlet NSTextField *userIDText;

appdelegate 在 Interface Builder 中连接到窗口和文本字段,然后连接到 .m 文件:(

@synthesize userIDText;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [userIDText setStringValue:@"user"];
    NSLog(@"UserIDTextString: %@", [userIDText stringValue]);

我还尝试了除 @"user" 作为字符串之外的其他内容) 日志仅显示(空)并且文本字段未更改。我做错了什么?我现在在互联网上搜索了大约 5 个小时,显然我错过了一些非常非常简单的东西。

I'm just at the beginning of a quite simple app (not really that into objective C yet) with a NSWindow containing a NSTextField. The appdelegate.h has this:

NSWindow *window;
NSTextField *userIDText;

@property (nonatomic, retain) IBOutlet NSWindow *window;
@property (nonatomic, retain) IBOutlet NSButton *nextButton;
@property (nonatomic, retain) IBOutlet NSTextField *userIDText;

the appdelegate is connected in Interface Builder to the window and the text field and then in the .m file:

@synthesize userIDText;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [userIDText setStringValue:@"user"];
    NSLog(@"UserIDTextString: %@", [userIDText stringValue]);

(I tried also other things than @"user" as string)
The log only shows (null) and the text field isn't changed. What am I doing wrong? I search the internets for about 5h now and it's obviously something really really simple I miss.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗫嚅 2024-12-01 01:24:38

您的 applicationDidFinishLaunching 方法在窗口加载之前被调用。这就是为什么 NSLog(@"UserIDTextString: %@", [userIDText stringValue]); 打印 null 并且您无法设置文本字段的值。尝试使用 awakeFromNib 方法。

Your applicationDidFinishLaunching method is called before the window is loaded. Thats why NSLog(@"UserIDTextString: %@", [userIDText stringValue]); prints null and you cant set value of the textfield. Try to use awakeFromNib method.

旧人 2024-12-01 01:24:38

刚刚发现你的问题,因为我也遇到了同样的问题。
问题是,如果您在预先创建的“MainMenu.xib”中创建一个带有按钮和内容的窗口,则不会将所有内容连接到“文件所有者”,而是连接到左侧单独生成的“应用程序代理”对象。

我很确定您已经解决了这个问题。
认为这可能对其有同样问题的其他人有帮助。

此致!

Just found your question as I had the same problem.
The issue was, that if you create a window with buttons and stuff in the precreated "MainMenu.xib", you don't connect everything to "File's Owner" but to the separately generated "App Delegate" object on the left side.

I'm pretty sure you have already fixed the issue.
Thought it might be helpful for someone else having the same problem.

Best regards!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文