正确的方法为UIView子类加载Nib [英] Correct way to load a Nib for a UIView subclass

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

问题描述

我知道之前已经问过这个问题,但答案是矛盾的,我很困惑,所以请不要激怒我。

I am aware this question has been asked before but the answers are contradicting and I am confused, so please don't flame me.

我想要一个在我的应用程序中可重复使用的 UIView 子类。我想用nib文件描述接口。

I want to have a reusable UIView subclass throughout my app. I want to describe the interface using a nib file.

现在让我们说它是一个带有活动指示器的加载指示器视图。我想在某个事件上实例化此视图并动画到视图控制器的视图。我可以用编程方式描述视图的界面没有问题,以编程方式创建元素并在init方法中设置它们的框架等。

Now let's say it's a loading indicator view with an activity indicator in it. I would like on some event to instantiate this view and animate in to a view controller's view. I could describe the view's interface no problem programmatically, creating the elements programmatically and setting their frame inside an init method etc.

我怎么能用笔尖做到这一点?维护界面构建器中给出的大小而不必设置框架。

How can I do this using a nib though? Maintaining the size given in interface builder without having to set a frame.

我设法这样做,但我确定它是错的(它只是带有选择器的视图):

I've managed to do it like this, but I'm sure it is wrong (it's just a view with a picker in it):

 - (id)initWithDataSource:(NSDictionary *)dataSource {
        self = [super init];
        if (self){
            self = [[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"%@", [self class]] owner:self options:nil] objectAtIndex:0];
            self.pickerViewData = dataSource;
            [self configurePickerView];
        }
        return self;
    }

但我覆盖了自己,当我实例化它时:

But I'm overwriting self, and when I instantiate it:

FSASelectView *selectView = [[FSASelectView alloc] initWithDataSource:selectViewDictionary];
    selectView.delegate = self;

    selectView.frame = CGRectMake(0, self.view.bottom + 50, [FSASelectView width], [FSASelectView height]);

我必须手动设置框架而不是从IB获取框架。

I have to manually set the frame rather than have it picked up from IB.

编辑:我想在视图控制器中创建此自定义视图,并有权控制视图的元素。我不想要新的视图控制器。

I want to create this custom view in a view controller, and have access to control the view's elements. I don't want a new view controller.

谢谢

编辑:我不要知道这是最佳做法,我确定不是,但这就是我做的方式:

FSASelectView *selectView = [[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"%@",[FSASelectView class]] owner:self options:nil] objectAtIndex:0];
    selectView.delegate = self;
    [selectView configurePickerViewWithData:ds];
    selectView.frame = CGRectMake(0, self.view.bottom + 50, selectView.width, selectView.height);
    selectView.alpha = 0.9;
    [self.view addSubview:selectView];
    [UIView animateWithDuration: 0.25 delay: 0 options:UIViewAnimationOptionAllowUserInteraction |UIViewAnimationOptionCurveEaseInOut animations:^{
                            selectView.frame = CGRectMake(0, self.view.bottom - selectView.height, selectView.width, selectView.height);
                            selectView.alpha = 1;
                        } completion:^(BOOL finished) {
                        }];

仍需要正确的做法

是否应该使用视图控制器和带有nib名称的init完成?我应该在代码中的某些UIView初始化方法中设置nib吗?或者我做得好吗?

Should this have been done using a view controller and init with nib name? Should I have set the nib in some UIView initialisation method in the code? Or is what I have done ok?

推荐答案

MyViewClass *myViewObject = [[[NSBundle mainBundle] loadNibNamed:@"MyViewClassNib" owner:self options:nil] objectAtIndex:0]

我'使用它来初始化我可以使用的可重复使用的自定义视图。

I'm using this to initialise the reusable custom views I have.

请注意,您可以使用firstObject在那里,它有点清洁。 firstObject是NSArray和NSMutableArray的一个方便方法。

Note that you can use "firstObject" at the end there, it's a little cleaner. "firstObject" is a handy method for NSArray and NSMutableArray.

这是加载一个xib用作表头的典型示例。在你的文件YourClass.m

Here's a typical example, of loading a xib to use as a table header. In your file YourClass.m

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[NSBundle mainBundle] loadNibNamed:@"TopArea" owner:self options:nil].firstObject;
}

通常,在 TopArea.xib ,您可以单击文件所有者,然后将文件所有者设置为YourClass 。然后实际上在YourClass.h中 你将拥有IBOutlet属性。在 TopArea.xib 中,您可以将控件拖到这些商店。

Normally, in the TopArea.xib, you would click on File Owner and set the file owner to YourClass. Then actually in YourClass.h you would have IBOutlet properties. In TopArea.xib, you can drag controls to those outlets.

不要忘记在 TopArea.xib ,您可能必须点击查看本身并将其拖到某个插座,这样您就可以根据需要控制它。 (一个非常有价值的提示是,当您为表格单元格行执行此操作时,您必须这样做 - 您必须将视图本身连接到代码中的相关属性。)

Don't forget that in TopArea.xib, you may have to click on the View itself and drag that to some outlet, so you have control of it, if necessary. (A very worthwhile tip is that when you are doing this for table cell rows, you absolutely have to do that - you have to connect the view itself to the relevant property in your code.)

这篇关于正确的方法为UIView子类加载Nib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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