iOS loadNibNamed 混淆,最佳实践是什么? [英] iOS loadNibNamed confusion, what is best practice?

查看:28
本文介绍了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(称为视图)和一个 UILabel(称为 myLabel)的 IBOutlets.我添加了 'view' 属性,因为网上的一些例子似乎表明你需要这个,它实际上解决了我因为找不到 view 属性而崩溃的问题,我猜甚至在 UIView 父类中也没有.
  • 我有一个名为 MyClass.xib 的 XIB 文件,它的 File's Owner 自定义类是 MyClass,在该类的 .h 和 .m 存在后正确预填充.

我的 init 方法是我遇到问题的地方.

My init method is where I'm having issues.

我尝试使用 NSBundle mainBundle 的loadNibNamed"方法并将所有者设置为self",希望我能够创建视图的一个实例,并且它会自动让它的插座与我班上的插座相匹配(我知道如何做到这一点,我很小心).然后我想我想让自我"等于那个笔尖中索引 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];

或类似的东西.

我感觉我在这里做错了,但是在线示例在 init 方法中发生了类似的事情,但是他们将该子视图 0 分配给视图属性并将其添加为子视图 - 但不是那么一共有两个 MyClass 实例?一个基本上没有链接到 IBOutlets,包含通过 loadNibNamed 实例化的子 MyClass?或者充其量,它不是一个带有额外中间 UIView 的 MyClass 实例,其中包含我最初想要作为 MyClass 的直接子级的所有 IBOutlets?当涉及到执行诸如 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]; 

提前感谢您的帮助.

推荐答案

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

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天全站免登陆