iOS loadNibNamed混乱,最佳做法是什么? [英] iOS loadNibNamed confusion, what is best practice?

查看:734
本文介绍了iOS loadNibNamed混乱,最佳做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉为我自己的UIView子类创建XIB的大部分过程,但并非一切都适合我 - 这主要与IBOutlets链接有关。我可以让他们以迂回的方式工作。

I'm familiar with most of the process of creating an XIB for my own UIView subclass, but not everything is working properly for me - it's mostly to do with the IBOutlets linking up. I can get them to work in what seems like a roundabout way.

我的设置是:


  • 我有MyClass.h和MyClass.m。他们有一个用于UIView的IBOutlets(称为视图)和一个UILabel(称为myLabel)。我添加了'view'属性,因为在线的一些示例似乎表明你需要这个,它实际上解决了我遇到崩溃的问题,因为它无法找到view属性,我想甚至不在UIView父类中。

  • 我有一个名为MyClass.xib的XIB文件,其文件所有者自定义类是MyClass,在我的.h和.m之后正确预填充该类。

我的初始化方法是我遇到问题的地方。

My init method is where I'm having issues.

我试图使用NSBundle mainBundle的'loadNibNamed'方法并将所有者设置为'self',希望我创建一个视图实例,它会自动将它的出口与我班级的出口相匹配(我知道如何做到这一点和我很小心)。然后我想我想让'self'等于那个nib中索引0的子视图,而不是做

I tried to use the NSBundle mainBundle's 'loadNibNamed' method and set the owner to 'self', hoping that I'd be creating an instance of the view and it'd automatically get its outlets matched to the ones in my class (I know how to do this and I'm careful with it). I then thought I'd want to make 'self' equal to the subview at index 0 in that nib, rather than doing

self = [super init];

或类似的东西。

I感觉我在这里做错了,但是在线的例子在init方法中有类似的事情,但他们将子视图0分配给view属性并将其添加为子项 - 但是那不是总共两个MyClass实例?一个基本上与IBOutlets无关,包含通过loadNibNamed实例化的子MyClass?或者充其量,它不是一个MyClass实例,带有一个额外的中间UIView,其中包含我最初想要的所有IBOutlets作为MyClass的直接子项吗?当涉及到像instanceOfMyClass.frame.size.width这样的事情时,这会引起一些轻微的烦恼,因为它返回0,当引入的子UIView返回我正在寻找的实际帧大小时。

I sense that I'm doing things wrong here, but examples online have had similar things going on in the init method, but they assign that subview 0 to the view property and add it as a child - but is that not then a total of two MyClass instances? One essentially unlinked to IBOutlets, containing the child MyClass instantiated via loadNibNamed? Or at best, is it not a MyClass instance with an extra intermediary UIView containing all the IBOutlets I originally wanted as direct children of MyClass? That poses a slight annoyance when it comes to doing things like instanceOfMyClass.frame.size.width, as it returns 0, when the child UIView that's been introduced returns the real frame size I was looking for.

我在做错的事情是我在init方法中搞乱了loadNibNamed吗?我应该做更像这样的事吗?

Is the thing I'm doing wrong that I'm messing with loadNibNamed inside an init method? Should I be doing something more like this?

MyClass *instance = [[MyClass alloc] init];
[[NSBundle mainBundle] loadNibNamed:@"MyClass" owner:instance options:nil];  

或者这样?

MyClass *instance = [[[NSBundle mainBundle] loadNibNamed:@"MyClass" owner:nil options:nil] objectAtIndex:0]; 

预先感谢您的任何帮助。

Thanks in advance for any assitance.

推荐答案

第二个选项是正确的选项。你可以做的最具防御性的代码是这样的:

The second option is the correct one. The most defensive code you could do is like this:

+ (id)loadNibNamed:(NSString *)nibName ofClass:(Class)objClass {
    if (nibName && objClass) {
        NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName 
                                                         owner:nil 
                                                       options:nil];            
        for (id currentObject in objects ){
            if ([currentObject isKindOfClass:objClass])
                return currentObject;
        }
    }

    return nil;
}

并且这样打电话:

MyClass *myClassInstance = [Utility loadNibNamed:@"the_nib_name" 
                                         ofClass:[MyClass class]]; 
// In my case, the code is in a Utility class, you should 
// put it wherever it fits best

我假设您的MyClass是 UIView 的子类?如果是这种情况,那么您需要确保.xib的 UIView 实际上是MyClass类。在选择视图

I'm assuming your MyClass is a subclass of UIView? If that's the case, then you need to make sure that the UIView of your .xib is actually of MyClass class. That is defined on the third Tab on the right-part in the interface builder, after you select the view

这篇关于iOS loadNibNamed混乱,最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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