如何以编程方式创建 NSTextField?

发布于 2025-01-02 12:26:32 字数 447 浏览 3 评论 0原文

我想以编程方式创建 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 技术交流群。

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

发布评论

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

评论(2

呆萌少年 2025-01-09 12:26:32

在 Cocoa 中以编程方式创建 UI 元素非常简单。

它涉及两个步骤:

  1. 创建视图并为其设置框架。

  2. 将其作为子视图添加到任何超级视图。

以下片段将帮助您更好地理解。

NSRect frameRect = NSMakeRect(20,20,40,40); // 这将根据您需要的大小而改变

NSTextField *myTextField = [[NSTextField alloc] initWithFrame:frameRect];

[myView addSubview:myTextField];

Creating UI elements programmatically is very simple in Cocoa.

It involves two steps:

  1. Create the view and set a frame to it.

  2. Add it as a subview to any super view.

Following snippet will you help you understand better.

NSRect frameRect = NSMakeRect(20,20,40,40); // This will change based on the size you need

NSTextField *myTextField = [[NSTextField alloc] initWithFrame:frameRect];

[myView addSubview:myTextField];
幼儿园老大 2025-01-09 12:26:32

看起来您正在创建一个 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 more NSWindow 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.

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