如何以编程方式将 UI 元素添加到现有 nib 文件

发布于 2025-01-03 23:21:47 字数 344 浏览 4 评论 0原文

我想知道如何以编程方式将 UI 元素添加到现有的 nib 文件中。

如果我在 loadView 方法中以编程方式创建视图并添加如下代码,标签将正确显示。

self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];

UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,2,150,100)];

[self.view addView:lbl];

但是如何将标签添加到现有的 nib 文件中呢?

I wonder how to add UI elements programatically to existing nib files.

If I create a view programatically in loadView method and I add code like the following, the label displays correctly.

self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];

UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,2,150,100)];

[self.view addView:lbl];

But how to add the label to an existing nib file?

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

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

发布评论

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

评论(3

彡翼 2025-01-10 23:21:47

正如 Paul.s 指出的,您需要在 viewDidLoad 方法中执行自定义代码。

来自苹果文档。

此方法在视图控制器加载其后调用
相关视图存入内存。无论什么情况都会调用此方法
视图是否存储在 nib 文件中或创建
以编程方式在 loadView 方法中。这种方法最常用
用于对视图执行额外的初始化步骤
从 nib 文件加载。

所以,在你的控制器中你可以这样做:

- (void)viewDidLoad
{
   [super viewDidLoad];

   // your other views here

   // call addSubview method on self.view
}

为什么要这样做?因为在这里您可以确定视图已加载到内存中并且插座已正确设置。

相反,关于loadView方法

如果您重写此方法以手动创建视图,
您应该这样做并将层次结构的根视图分配给
查看属性。 (您创建的视图应该是唯一的实例,并且
不应与任何其他视图控制器对象共享。)
此方法的自定义实现不应调用 super。

一个例子是:

- (void)loadView
{
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    UIView *contentView = [[UIView alloc] initWithFrame:applicationFrame];
    contentView.backgroundColor = [UIColor whiteColor];

    self.view = contentView; 

    // call addSubview method on self.view
}

我建议您阅读 iOS 版控制器编程指南

希望有帮助。

As Paul.s pointed out, you need to perform your custom code in viewDidLoad method.

From Apple documentation.

This method is called after the view controller has loaded its
associated views into memory. This method is called regardless of
whether the views were stored in a nib file or created
programmatically in the loadView method. This method is most commonly
used to perform additional initialization steps on views that are
loaded from nib files.

So, in your controller you could do this:

- (void)viewDidLoad
{
   [super viewDidLoad];

   // your other views here

   // call addSubview method on self.view
}

Why do you do this? Because here you are sure that view has loaded in memory and outlets has been set correctly.

On the contrary, about loadView method

If you override this method in order to create your views manually,
you should do so and assign the root view of your hierarchy to the
view property. (The views you create should be unique instances and
should not be shared with any other view controller object.) Your
custom implementation of this method should not call super.

An example could be:

- (void)loadView
{
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    UIView *contentView = [[UIView alloc] initWithFrame:applicationFrame];
    contentView.backgroundColor = [UIColor whiteColor];

    self.view = contentView; 

    // call addSubview method on self.view
}

I suggest you to read View Controller Programming Guide for iOS.

Hope it helps.

乜一 2025-01-10 23:21:47

AFAIK,以编程方式修改 nib 文件是不可能的。

您可以将视图添加到 UIViewController 方法的 viewDidLoad 中。

AFAIK, to modify nib file programmatically is not possible.

You can add view into viewDidLoad of UIViewController method.

迟到的我 2025-01-10 23:21:47

里面viewDidLoad

UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,2,150,100)];
[self.view addSubView:lbl];

Inside viewDidLoad

UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,2,150,100)];
[self.view addSubView:lbl];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文