programmatically loading object from subclass of NSView from nib

后端 未结 5 806
闹比i
闹比i 2020-12-24 02:56

How do I correctly load an object that is a subclass of NSView using a Xib?

I want it to be loaded dynamically not from the beginning so I made a MyView.Xib From MyD

5条回答
  •  天命终不由人
    2020-12-24 03:40

    You can refer to this category

    https://github.com/peterpaulis/NSView-NibLoading-/tree/master

    + (NSView *)loadWithNibNamed:(NSString *)nibNamed owner:(id)owner class:(Class)loadClass {
    
        NSNib * nib = [[NSNib alloc] initWithNibNamed:nibNamed bundle:nil];
    
        NSArray * objects;
        if (![nib instantiateWithOwner:owner topLevelObjects:&objects]) {
            NSLog(@"Couldn't load nib named %@", nibNamed);
            return nil;
        }
    
        for (id object in objects) {
            if ([object isKindOfClass:loadClass]) {
                return object;
            }
        }
        return nil;
    }
    

提交回复
热议问题