来自另一个UIViewController的nib中的nib的自定义UIView - IBOutlets是nil [英] Custom UIView from nib inside another UIViewController's nib - IBOutlets are nil

查看:91
本文介绍了来自另一个UIViewController的nib中的nib的自定义UIView - IBOutlets是nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个自定义UIView,它保存对自己的IBOutlets的引用。然后我想将这个自定义UIView放入另一个笔尖。

I'm trying to create a custom UIView which holds references to its own IBOutlets. I then want to put this custom UIView into another nib.

我在自定义UIView的 awakeFromNib 方法中做了一些额外的逻辑。不幸的是,当我尝试在 awakeFromNib 中访问 IBOutlets 时,它们是零。

I'm doing some additional logic in the custom UIView's awakeFromNib method. Unfortunately, when I try to access the IBOutlets in awakeFromNib, they are nil.

以下是设置:


  1. 我有 UIView 子类, CustomView

  2. 我有一个自定义的 .xib 文件,其中包含三个子视图

  3. 在另一个nib(属于视图控制器)中,我将 UIView 拖到视图上,然后更改自定义类 CustomView

  4. 我尝试在 CustomView 在IB中将nib转换为自定义类 CustomView 并将 IBOutlets 连接到视图,但它们仍为零。

  5. 我尝试将文件所有者设置为 CustomView 并将 IBOutlets 连接到文件所有者,但他们仍然是零。

  6. 我还尝试使用另一个 IBOutlet UIView *视图,然后将其作为子视图添加到 self in awakeFromNib 但这也没有做任何事情。

  1. I have a UIView subclass, CustomView.
  2. I have a custom .xib file with three subviews
  3. In the other nib (that belongs to the view controller), I have dragged a UIView onto the view, and then changed the custom class to CustomView.
  4. I tried setting the view in the CustomView nib in IB to a custom class CustomView and connecting the IBOutlets to the view, but they were still nil.
  5. I tried setting file owner to CustomView and connecting the IBOutlets to file's owner, but they were still nil.
  6. I also tried using another IBOutlet UIView *view and then adding that as a subview to self in awakeFromNib but that also didn't do anything.

这是代码:

// CustomView.h
@interface CustomView : UIView

@property (nonatomic, retain) IBOutlet UITextField *textField;
@property (nonatomic, retain) IBOutlet UIView *subview1;
@property (nonatomic, retain) IBOutlet UIView *subview2;

// CustomView.m
@implementation CustomView
@synthesize textField, subview1, subview2; 

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];

    if (self) {
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setup];
}

- (void)setup {
    // Fails because self.textField is nil
    self.textField.text = @"foo";
}


推荐答案

我最终使用了这些步骤在最近的编辑中此处他们工作得很漂亮。

I ended up using the steps in the most recent edit here and they worked beautifully.

您使用普通的 UIView 作为 xib

然后将文件所有者设置为自定义子类( CustomView )。

You then set file's owner to the custom subclass (CustomView).

最后,你添加一行:

[self addSubview:[[[UINib nibWithNibName:@"CustomView" bundle:nil] instantiateWithOwner:self options:nil] objectAtIndex:0]];

if(self!= nil)阻止 initWithCoder initWithFrame

瞧! IBOutlets 已连接好并准备好在通话结束后继续。对解决方案非常满意,但很难挖掘出来。

Voila! The IBOutlets are hooked up and ready to go after the call. Really pleased with the solution, but it was very difficult to dig up.

希望这有助于其他任何人。

Hope this helps anyone else.

编辑:我将链接更新为未死的链接。由于我从未拼写过完整的代码,因此修改后的内容如下:

EDIT: I updated the link to one that isn't dead. Since I never spelled out the full code, here is what it looks like after modification:

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        UIView *nib = [[[UINib nibWithNibName:@"CustomView" bundle:nil] instantiateWithOwner:self options:nil] objectAtIndex:0];
        [self addSubview:nib];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];

    if (self) {
        UIView *nib = [[[UINib nibWithNibName:@"CustomView" bundle:nil] instantiateWithOwner:self options:nil] objectAtIndex:0];
        [self addSubview:nib];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setup];
}

- (void)setup {
    // Doesn't fail because life is awesome
    self.textField.text = @"foo";
}

这种模式已经变得非常普遍,我实际上在<$ c上创建了一个类别$ c> UIView 名为 UIView + Nib ,它实现了以下方法:

This pattern has become so common that I actually created a category on UIView called UIView+Nib, which implements the following method:

+ (UIView *)viewWithNibName:(NSString *)nibName owner:(id)owner {
    return [[[UINib nibWithNibName:nibName bundle:nil]
            instantiateWithOwner:owner options:nil]
            objectAtIndex:0];
}

所以上面的代码可以简化为:

So the above code can be simplified to:

[self addSubview:[UIView viewWithNibName:@"CustomView" owner:self]];

另请注意,上述代码可以进行更多重构,因为逻辑在 initWithFrame: initWithCoder:。希望有所帮助!

Note also that the above code can be refactored even more, since the logic is exactly the same in initWithFrame: and initWithCoder:. Hope that helps!

这篇关于来自另一个UIViewController的nib中的nib的自定义UIView - IBOutlets是nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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