How to use a xib and a UIView subclass together?

后端 未结 1 2035
故里飘歌
故里飘歌 2020-12-28 16:16

I\'d like to create a couple of custom views, sort of reusable UI components, and would prefer to not layout the UI in code in the UIView subclass. I\'d like to use a xib fo

相关标签:
1条回答
  • 2020-12-28 17:14

    Yes that's the way that it works when you're loading xibs that aren't parents to viewControllers

    Edit August 15, 2013:

    You can't always just assume that you're going to get exactly what you're looking for out of index 0 of the returned NSArray, which is what makes it good to use type checking.

    NSArray *xibArray = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:nil options:nil];
    MyCustomView *myView = nil;
    for (id xibObject in xibArray) {
    //Loop through array, check for the object we're interested in.
        if ([xibObject isKindOfClass:[MyCustomView class]]) {
            //Use casting to cast (id) to (MyCustomView *)
            myView = (MyCustomView *)xibObject;
        }
    }
    
    0 讨论(0)
提交回复
热议问题