如何以编程方式嵌入笔尖? [英] How to embed nibs programmatically?

查看:514
本文介绍了如何以编程方式嵌入笔尖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有UIScrollView的UIViewController。

I have a UIViewController with an UIScrollView in it.

然后,我创建了一个新的UIView子类,其中包含一些属性,我想通过编程方式添加到这个UIScrollView中,直到我的数据

Then I created a new UIView subclass with some properties that I want to programmatically add in this UIScrollView until my data ends.

应该看起来像这张照片的东西:

Something that should look like this picture:

我已经通过创建一个视图并将其类设置为自定义视图子类来读取其他主题,但是我想这样做

I have read other topics that does that by creating a view and setting its class to the custom view subclass, but I want to do it programmatically and its not working.

我该怎么做?

推荐答案

p>从您的图像看,您希望从一个笔尖加载视图,并将其添加为UIScrollView的子视图。为此,请查看 UINib文档

From your image it looks like you're looking to load views from a nib and add them as subviews of your UIScrollView. To do this have a look at the UINib documentation.

你想创建一个nib,并将其主视图设置为UIView子类的一个实例,然后在中加载nib viewDidLoad ,并将nib的视图添加到scrollview的subivews(我假设它是viewController视图的子视图)。

You want to create your nib and set it's main view to be an instance of your UIView subclass then load the nib in viewDidLoad of your viewController, and add the nib's views as subivews of your scrollview (which I'm assuming is a subview of your viewController's view).

您可以用 instantiateWithOwner:options:实例化一个笔尖。

You can instantiate a nib with instantiateWithOwner:options:.


此方法取消存档每个对象,初始化它,将其
属性设置为其配置的值,并重新建立与其他对象的任何
连接

This method unarchives each object, initializes it, sets its properties to their configured values, and reestablishes any connections to other objects

要从一个笔尖获取视图数组,您可以执行以下操作:

To get the array of views from a nib you do something similar to:

UINib *myNib = [UINib nibWithNibName:@"myNib" bundle:[NSBundle mainBundle]];
NSArray *viewsFromNib = [myNib instantiateWithOwner:nil options:nil];

我假设我们在一个UIViewController内,我们在(或之后) viewDidLoad中。然后,您将使用上面的数组,并将视图添加为scrollview的子视图。您可能需要设置这些视图的框架以使其正确放置,但这应该是微不足道的。

I'll assume we're inside a UIViewController and we're somewhere in (or after) viewDidLoad. You would then use the array from above and add the views as subviews of your scrollview. You may need to set the frames of these views to place them properly, but that should be trivial.

UIView *aView = [viewsFromNib objectAtIndex:0];
[self.scrollView addSubview:aView];

我希望这能让您处于正确的方向。

I hope that sets you in the right direction.

编辑:

如果您需要更多信息,您可能需要更深入地了解nibs如何工作来管理您的期望。与UINib文档相关联的是资源编程指南,特别是笔尖部分

If you want more information you may need to read deeper into how nibs work to manage your expectation. Linked with the UINib documentation is the 'Resource Programming Guide' in particular the nib section

这篇关于如何以编程方式嵌入笔尖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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