如何以编程方式将 UI 元素添加到现有 nib 文件
我想知道如何以编程方式将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 Paul.s 指出的,您需要在 viewDidLoad 方法中执行自定义代码。
来自苹果文档。
所以,在你的控制器中你可以这样做:
为什么要这样做?因为在这里您可以确定视图已加载到内存中并且插座已正确设置。
相反,关于
loadView
方法一个例子是:
我建议您阅读 iOS 版控制器编程指南。
希望有帮助。
As Paul.s pointed out, you need to perform your custom code in
viewDidLoad
method.From Apple documentation.
So, in your controller you could do this:
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
methodAn example could be:
I suggest you to read View Controller Programming Guide for iOS.
Hope it helps.
AFAIK,以编程方式修改 nib 文件是不可能的。
您可以将视图添加到 UIViewController 方法的 viewDidLoad 中。
AFAIK, to modify nib file programmatically is not possible.
You can add view into viewDidLoad of UIViewController method.
里面
viewDidLoad
Inside
viewDidLoad