Instantiating a UIView from a XIB file with itself as the file's owner?

半腔热情 提交于 2019-12-12 09:56:25

问题


Let's say I have a XIB file (MyView.xib) containing a single root UIView whose class is some custom UIView subclass (MyView).

I want to create an instance of MyView (in code) from the XIB file but I also want the file's owner to be the instance of MyView itself (so that I can link IBOutlets from the XIB to the code).

In other words, MyView is the class of the root view in the XIB and it's also set as the file's owner class.

I tried something like this inside the MyView implementation:

- (id)initFromNib {
    self = [[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil] firstObject];
    return self;
}

Then I realized that makes no sense because at that point self is nil, so the file's owner is set to nil (no good).

This is starting to feel like a chicken-egg problem because I need the file's owner to be the instance that is about to be created, before it gets created.

I then tried something like this:

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:CGRectMake(0.0, 0.0, 310, 166)];
    if (self) {
        self = [[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil] firstObject];
    }
    return self;
}

Clearly this was a temporary hack just to see if it would work, but no go. The view gets loaded and displays properly but the app crashes when I press one of the buttons (which have associated IBActions).

Am I missing something obvious? Or is my understanding flawed in some way?

The general problem is that I just want to have a custom UIView subclass with an associated XIB which contains the visual representation of that view, and I also want to be able to associate IBOutlets between the XIB and its corresponding UIView subclass.

Any and all suggestions are welcome.


回答1:


After some digging I found a workaround. Basically instead of linking my IBOutlets and IBActions to the file's owner I linked them directly to the root view's class (which has the IB-properties exposed via the custom subclass).

The key was to just not rely on the file's owner and instead connect outlets and actions directly to the custom view.

This is the initializer I ended up going with:

- (instancetype)initFromNib {
    return [[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:nil options:nil] firstObject];
}

This seems to be working fine now.

UPDATE

I've create a category on UIView for initializing views from XIBs (it assumes the XIB name is the same as the class name).

UIView+Initialization.h

#import <UIKit/UIKit.h>

@interface UIView (Initialization)

- (instancetype)initFromNib;

@end

UIView+Initialization.m

#import "UIView+Initialization.h"

@implementation UIView (Initialization)

- (instancetype)initFromNib {
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:nil options:nil] firstObject];
}

@end

You can then simply do:

MyView *myView = [[MyView alloc] initFromNib];

Assuming you have a MyView.xib file with a root view who's class is MyView.



来源:https://stackoverflow.com/questions/23797326/instantiating-a-uiview-from-a-xib-file-with-itself-as-the-files-owner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!