如何以编程方式创建 NSTextField?
我想以编程方式创建 NSTextField
。我是 Mac 应用程序开发新手。
有人能帮我吗?
更新:
我尝试了这段代码
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSView *myView = [[NSView alloc] init];
NSRect frameRect = NSMakeRect(20,20,100,140);
NSTextField *myTextField = [[NSTextField alloc] initWithFrame:frameRect];
[myView addSubview:myTextField];
}
这什么也没做。如果我有任何错误,请纠正我。
谢谢。
I want to create NSTextField
programmatically. I am new to Mac App Development.
Can anyone help me in this ?
Update :
I tried this code
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSView *myView = [[NSView alloc] init];
NSRect frameRect = NSMakeRect(20,20,100,140);
NSTextField *myTextField = [[NSTextField alloc] initWithFrame:frameRect];
[myView addSubview:myTextField];
}
This does nothing. Please correct me if i'm wrong anywhere.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Cocoa 中以编程方式创建 UI 元素非常简单。
它涉及两个步骤:
创建视图并为其设置框架。
将其作为子视图添加到任何超级视图。
以下片段将帮助您更好地理解。
Creating UI elements programmatically is very simple in Cocoa.
It involves two steps:
Create the view and set a frame to it.
Add it as a subview to any super view.
Following snippet will you help you understand better.
看起来您正在创建一个 NSTextField ,但您没有将其添加到视图层次结构中可见的任何位置。您的应用程序可能包含一个或多个 NSWindow 实例;如果您希望视图出现在特定窗口中,则应将其添加为该窗口内容视图的子视图。
It looks like you're creating an
NSTextField
OK, but you're not adding it to anywhere visible in the view hierarchy. Your app probably contains one or moreNSWindow
instances; if you want a view to appear in a particular window, you should add it as a subview of that window's content view.