与 XIB 相比,基于代码的 UIView 的重用
假设我有一个 UIView,我想在多个视图控制器中重用它,因为它是一个相当通用的 UIView 对象,具有很高的可重用性价值(如 UITextField
或 UISegmentedControl
对象)。根据我的理解,如果我用代码编写 UIView,而不是使用 Interface Builder 制作 XIB,那么重用此 UIView 会容易得多。我得出这个结论是因为如果 UIView 是用代码编写的,那么我可以简单地在任何视图控制器中初始化该 UIView 的新实例:
MyGreatReusableView *greatReusableView = [[MyGreatReusableView alloc] initWithFrame:CGRectMake(...)];
...然后直接访问该 UIView 的属性和属性,就像使用库存 UIView 一样诸如 UITextField
之类的控件及其 .text
、.textColor
等属性。
但是,如果我将 UIView 创建为 XIB,则它必须(通过文件所有者或 XIB 中视图对象上的 IBOutlet)绑定到特定视图控制器,因此只能在该视图控制器中使用。此外,我只能使用连接到该视图控制器的 IBOutlet 访问 XIB 上控件的属性。
我认为我完全误解了有关 XIB 文件的一些内容,因为这似乎确实是一个相当大的限制!如果有人可以澄清是否可以在多个视图控制器中重用 XIB 文件,如果可以,请提供基于代码的解决方案与基于 XIB 的解决方案的示例,我将非常感激。
预先感谢您的任何帮助!
Say that I have a UIView which I would like to re-use in multiple view controllers, given that it is a fairly generic UIView object with high re-usability value (like a UITextField
or UISegmentedControl
object). From my understanding it would be much easier to reuse this UIView if I wrote the UIView in code, rather than making a XIB using Interface Builder. I have reached this conclusion because if the UIView is written in code then I can simply initialize a new instance of that UIView in any view controller:
MyGreatReusableView *greatReusableView = [[MyGreatReusableView alloc] initWithFrame:CGRectMake(...)];
...and then directly access the properties and attributes of that UIView, just like with the stock UIView controls such as UITextField
with it's .text
, .textColor
, etc. properties.
However, if I create the UIView as a XIB then it must be tied (through either the File's Owner or an IBOutlet on the View object in the XIB) to a specific view controller, and therefore can only be used in that view controller. Furthermore, I can only access the properties of controls on the XIB using the IBOutlets which are connected to that view controller.
I am thinking that I have completely misunderstood something with regards to XIB files, as this does seem to be quite a limitation! If someone could provide clarification on whether it is possible to re-use XIB files in multiple view controllers and, if so, provide an example of a code-based solution vs. a XIB-based solution I would be very much appreciative.
Thanks in advance for any assistance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无需设置可重用视图的 IBOutlet 的替代解决方案是为要使用的子视图分配特定标记。然后就可以获取带有标签的视图。
例如,当您想要创建带有标签和文本字段的视图时。执行此操作的过程是:
为标签和文本字段分配不同的标签(假设为 10 和 11)
为此 xib 创建一个头文件并为标签定义宏
#define kReusableViewNibName @"ReusableViewNibName"
#define kLabelTag 10
#define kTextFieldTag 11
您现在可以在任何您想要的视图控制器中加载 xib:
An alternative solution, without the need of setting the IBOutlets of the reusable view would be by assigning the subviews you want to use with a specific tag. Then you could obtain the views with the tag.
For example when you want to create a view with a label and a text field. The process for doing this would be:
Assign the label and the text field different tags (let's say 10 and 11)
Create a header file for this xib and define macros for the tags
#define kReusableViewNibName @"ReusableViewNibName"
#define kLabelTag 10
#define kTextFieldTag 11
You can now load the xib in any view controller you want to:
确实可以在多个视图控制器中使用相同的视图。你走在正确的轨道上。关键是您不必使用视图所绑定的视图控制器。这是我对可重用表格单元格(包含滑块等内容)所做的操作
<前><代码>
@实现TableCellFactory
+(UITableViewCell*) createCellInstanceFromXIBName:(NSString*)name {
UIViewController* tmp = [[UIViewController alloc] initWithNibName:名称包:nil];
UITableViewCell* 单元格 = (UITableViewCell*)[临时视图];
DebugAssert([[单元格类] isSubclassOfClass:[UITableViewCell 类]], @"XIB 视图单元格不是 UITableViewCell 的子类; xib 名称:%@",name);
DebugAssert(cell != nil,@"cell 为 nil -- Xib 名称:%@",name);
[细胞保留];
[临时发布];
返回单元格;
}
@结尾
It is indeed possible to use the same view in multiple view controllers. You were on the right track. The key is that you don't have to use the view controller to which the view is tied. Here is what I do for re-usable table cells (that contain things like sliders)