ios创建然后动态添加UI元素 [英] ios create and then dynamically add UI elements

查看:80
本文介绍了ios创建然后动态添加UI元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否可行,但听起来应该是这样。

I don't know if this is possible but it sounds like something that should be.

我想做的是,创建UIViews(或UIViewControllers不是确实如何工作)在Interface Builder中说...其中10个,并根据用户选择使用它们。

What i want to do is, to create UIViews (or UIViewControllers not really sure how it would work) in Interface Builder say... 10 of them, and use them according to what the user choose to.

例如在状态A中,我想向他展示小部件1,2和3;
状态B小部件3,5和7等。

For example in state A i wanna show him widgets 1,2 and 3; in state B widgets 3,5 and 7 etc.

编辑:我的意思是状态,是根据用户在前面的步骤中选择的,它们都导致相同的UIViewController,但它应该根据上述状态呈现不同。现在我不能为每个不同的组合创建不同的UIViewController:)

what i mean by state, is that according to what the user chose in the previous steps, they all lead to the same UIViewController but it should present itself differently according to the aforementioned "state". And now I can't create a different UIViewController for every different combination :)

并且还能够将它们放在父视图中是必须的。

And also being able to position them in the parent view is a must.

有任何建议吗?

推荐答案

好的,所以在回答你的评论时,你可以确实做你想要的。为自定义视图创建一个单独的nib文件,例如MyView.xib,并使文件所有者的类成为视图控制器类的任何内容。

OK, so in answer to your comment, you can indeed do just what you want. Create a separate nib file for your custom view, say MyView.xib, and make the File's Owner's class to be whatever your view controller class is.

然后在视图控制器中,为自定义视图创建 IBOutlet 属性:

Then in your view controller, create an IBOutlet property for the custom view:

@property (nonatomic, assign) IBOutlet MyView * myView;

在nib文件中,挂钩 myView 视图控制器的出口,到顶层视图。

In the nib file, hook up the myView outlet of the view controller, to the top level view.

然后在代码中加载视图,您只需要:

Then to load the view in code, all you need to do is:

[[NSBundle mainBundle] )loadNibNamed: @"MyView" owner: self options: nil];

并且将加载nib并分配给 myView 。然后您可以将其添加为子视图。

and the nib will be loaded, and assigned to myView. Then you can add it as a subview.

要使其与另一个视图控制器一起使用,只需确保它具有相同的命名插座,请设置 loadNibNamed:调用,它只会工作。

To make this work with another view controller, just make sure it has the same named outlet, make the loadNibNamed: call, and it will just work.

这篇关于ios创建然后动态添加UI元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆