带有自己的 XIB 的 UIView 子类 [英] UIView subclass with its own XIB
问题描述
我创建了一个自定义 UIView 子类,并且不想在 UIView 子类中的代码中布局 UI.我想为此使用xib.所以我所做的是以下.
I created a custom UIView subclass, and would prefer to not layout the UI in code in the UIView subclass. I'd like to use a xib for that. So what I did is the following.
我创建了一个ShareView"类,它是 UIView 的子类.我创建了一个 XIB 文件,其文件所有者设置为ShareView".然后我链接了我在ShareView.h"中声明的一些网点.
I created a class "ShareView" which subclasses UIView. I created a XIB file with its file's owner set to "ShareView". Then I link some outlets I declared in my "ShareView.h".
接下来我有一个 ViewController,MainViewController,它将 ShareView 添加为子视图.带有此代码:
Next I have a ViewController, MainViewController, which adds the ShareView as a subview. whith this code:
NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"ShareView" owner:nil options:nil];
UIView *fv = [[arr objectAtIndex:0] retain];
fv.frame = CGRectMake(0, 0, 320, 407);
[self.view addSubview:fv];
但现在我在 ShareView 中声明的出口出现 NSUnknownKeyException 错误.
But now I get NSUnknownKeyException errors on the outlets I declared in my ShareView.
我做这一切的原因是因为我想要一个 UIView,在单独的 XIB 文件中有它自己的逻辑.我在几个地方读到 ViewControllers 仅用于管理全屏,即不是屏幕的一部分......那我做错了什么?我希望我的 ShareView 逻辑在一个单独的类中,所以我的 MainController 类不会因 ShareView 的逻辑而变得臃肿(我认为这是解决这个问题的一个选项?)
The reason I did all this is because I want a UIView, with its own logic in a seperate XIB file. I read in several places that ViewControllers are only used to manage a full screen, i.e. not parts of a screen... So what am I doing wrong? I want my logic for ShareView in a seperate class, so my MainController class doesn't get bloated with logic from ShareView (which I think is an aption to solve this problem?)
推荐答案
ThomasM,
我们对在自定义视图中封装行为也有类似的想法(例如,一个带有最小/最大/当前值伴随标签的滑块,值更改事件也由控件内部处理).
We had similar ideas about encapsulating behavior inside a custom view (say, a slider with companion labels for min/max/current values, with value-changed events also handled by the control internally).
在我们当前的最佳实践中,我们将在 Interface Builder (ShareView.xib
) 中设计 ShareView,如 Eimantas 在他的回答中所述.然后我们将 ShareView 嵌入到 MainViewController.xib
中的视图层次结构中.
In our current best-practice, we would design the ShareView in Interface Builder (ShareView.xib
), as described by Eimantas in his answer. We then embed the ShareView to the view hierarchy in MainViewController.xib
.
我写了我们如何将自定义视图 Nib 嵌入其他 Nib 中iOS 开发者博客.关键是在自定义视图中覆盖 -awakeAfterUsingCoder:
,将从 MainViewController.xib 加载的对象替换为从嵌入式" Nib (ShareView.xib) 加载的对象.
I wrote up how we embed custom-view Nibs inside other Nibs in our iOS developer blog. The crux is overriding -awakeAfterUsingCoder:
in your custom view, replacing the object loaded from MainViewController.xib with the one loaded from the "embedded" Nib (ShareView.xib).
大致如下:
// ShareView.m
- (id) awakeAfterUsingCoder:(NSCoder*)aDecoder {
BOOL theThingThatGotLoadedWasJustAPlaceholder = ([[self subviews] count] == 0);
if (theThingThatGotLoadedWasJustAPlaceholder) {
// load the embedded view from its Nib
ShareView* theRealThing = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ShareView class]) owner:nil options:nil] objectAtIndex:0];
// pass properties through
theRealThing.frame = self.frame;
theRealThing.autoresizingMask = self.autoresizingMask;
[self release];
self = [theRealThing retain];
}
return self;
}
这篇关于带有自己的 XIB 的 UIView 子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!