日々精進

新しく学んだことを書き留めていきます

XIBからNSViewのサブクラスのインスタンスを作る

以下のようにする。iOSの場合とちょっと違うので注意。

- (id)init {
    NSNib *nib = [[NSNib alloc] initWithNibNamed:@"View" bundle:nil];
    NSArray *topLevelObjects;
    if ([nib instantiateNibWithOwner:self topLevelObjects:&topLevelObjects]) {
        for (NSObject *object in topLevelObjects) {
            if ([object isMemberOfClass:[View class]]) {
                self = object;
            }
        }
    }
    return self;
}

参考:objective c - programatically loading object from subclass of NSView from nib - Stack Overflow