gpt4 book ai didi

macos - 如何以编程方式创建 NSTextField?

转载 作者:行者123 更新时间:2023-12-03 16:27:15 26 4
gpt4 key购买 nike

我想以编程方式创建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];
}

这没有任何作用。如果我有任何错误,请纠正我。

谢谢。

最佳答案

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

它涉及两个步骤:

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

  2. 将其作为 subview 添加到任何 super View 中。

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

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];

关于macos - 如何以编程方式创建 NSTextField?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9128054/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com