UIView子类有自己的XIB [英] UIView subclass with its own XIB

查看:82
本文介绍了UIView子类有自己的XIB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义的UIView子类,并且不希望在UIView子类的代码中布局UI。我想用xib。所以我做的是以下内容。



我创建了一个类ShareView,它是UIView的子类。我创建了一个XIB文件,其文件所有者设置为ShareView。然后我链接我在ShareView.h中声明的一些出口。



接下来我有一个ViewController,MainViewController,它将ShareView添加为子视图。这段代码:

  NSArray * arr = [[NSBundle mainBundle] loadNibNamed:@ShareView所有者:nil选项:nil] ; 
UIView * fv = [[arr objectAtIndex:0] retain];
fv.frame = CGRectMake(0,0,320,407);
[self.view addSubview:fv];

但是现在我在ShareView中声明的插座上出现了NSUnknownKeyException错误。



我做这一切的原因是因为我想要一个UIView,在一个单独的XIB文件中有自己的逻辑。我在几个地方读到ViewControllers只用于管理全屏,即不是屏幕的一部分......
那么我做错了什么?我想在单独的类中使用ShareView的逻辑,所以我的MainController类不会因为ShareView的逻辑而膨胀(我认为这是解决这个问题的一种方法吗?)

解决方案

ThomasM,



我们对自定义视图中的行为进行封装有类似想法(例如,带有最小伴随标签的滑块) / max / current值,内部控件也处理值更改事件。



在我们当前的最佳实践中,我们将在Interface Builder中设计ShareView( ShareView.xib ),如Eimantas在回答中所述。然后我们将ShareView嵌入到 MainViewController.xib 中的视图层次结构中。



我写了我们在我们的iOS开发者博客中将自定义视图Nib嵌入其他Nib中。关键是在自定义视图中覆盖 -awakeAfterUsingCoder:,将从MainViewController.xib加载的对象替换为从嵌入式Nib(ShareView.xib)加载的对象。 / p>

以下内容:

  // ShareView.m 
- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder {
BOOL theThingThatGotLoadedWasJustAPlaceholder =([[self subviews] count] == 0);
if(theThingThatGotLoadedWasJustAPlaceholder){
//从其Nib加载嵌入视图
ShareView * theRealThing = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ShareView class])所有者:nil选项: nil] objectAtIndex:0];

//通过
传递属性theRealThing.frame = self.frame;
theRealThing.autoresizingMask = self.autoresizingMask;

[自我发布];
self = [theRealThing retain];
}
返回自我;
}


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.

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".

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];

But now I get NSUnknownKeyException errors on the outlets I declared in my 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).

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.

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).

Something along these lines:

// 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;
}

这篇关于UIView子类有自己的XIB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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