在初始化期间将 IBOutlet UIView 添加到数组
我最近对 UIViews 有点困惑,到目前为止我一直在很好地使用它们,但它们只是拒绝遵守!
我有一个 UIViewController,它包含 5 个不同的视图。我为这些视图创建了 IBOutlet,因为我想在运行时交换它们:
IBOutlet UIView *view1;
IBOutlet UIView *view2;
IBOutlet UIView *view3;
IBOutlet UIView *view4;
IBOutlet UIView *view5;
为了使它们更易于维护,我决定将它们全部保存在一个名为 viewArray 的数组中。现在我尝试将视图添加到数组中,如下所示:
viewArray = [NSArray arrayWithObjects:view1, view2, view3, view4, nil];
这是在我的 UIViewController 类的 init 函数中调用的。我已将所有 IBOutlet 链接到 xib/interface 文件中的相关视图,但它们似乎并未初始化。经过进一步调试,看起来视图在调用 init 函数之后才被初始化?
那么如何创建这些对象的数组呢?我需要在显示视图本身之前选择相关视图,因此 viewDidLoad 不是一个选项。
我知道您可以获取事物的标签并使用隐式设置它们:
imageExample = (UIImageView *)[self.view viewWithTag:100];
但这可以用于查找视图吗?因为它肯定会在最初初始化的视图(view1)中搜索标签?
感谢您的帮助, 亲切的问候, 埃利奥特
I'm getting a bit confused with UIViews recently, I have been using them fine up until this point and they are just refusing to be compliant!
I have a UIViewController, and this contains 5 different views. I have created IBOutlets for these views as I am wanting to swap them at runtime:
IBOutlet UIView *view1;
IBOutlet UIView *view2;
IBOutlet UIView *view3;
IBOutlet UIView *view4;
IBOutlet UIView *view5;
To make them easier to maintain I decided to keep them all within an array, called viewArray. Now I am trying to add the views to the array as follows:
viewArray = [NSArray arrayWithObjects:view1, view2, view3, view4, nil];
This is being called in the init function of my UIViewController class. I have linked up all the IBOutlets to their relevant views in the xib / interface file, but they do not appear to be initialized. Upon further debugging it looks like the views aren't initialized until after the init function is called?
So how can I create an array of these objects? I will need to select the relevant view before the view itself is shown, therefore viewDidLoad is not an option.
I know that you can grab the tags of things and implicitly set them using:
imageExample = (UIImageView *)[self.view viewWithTag:100];
But can this be used to find views, as surely it will be searching for the tags within the originally initialized view (view1)?
Thanks for any help in advanced,
Kind Regards,
Elliott
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以延迟初始化
viewArray
,但代价是必须使用self.viewArray
而不是不合格的viewArray
。操作方法如下:
在 .h 文件中:
在 .m 文件中:
You can initialize the
viewArray
lazily, for the price of having to useself.viewArray
instead of unqualifiedviewArray
.Here is how you can do it:
In the .h file:
In the .m file: