4

Current Setup

I have a subclass of a UIView that loads a content from a xib file - call it a XibOwnerClass. Also, among other classes, there is a class called Triangle which helps me to create triangles with or without a border, with different stroke or fill color etc. That class is designable in storyboard and some of its properties are defined as IBInspectable.

Currently, in my xib file, I use this triangle view and setup its inspectable properties through IB. And that is really cool and convenient... So if I look into xib, I will actually see the triangle view among other views.

So, lets go further. In order to use this XibOwnerClass, I drag the UIView element to the storyboard, and change its custom class to XibOwnerClass, so I get my designable properties specific for XibOwnerClass. So, I can setup all things in IB and when I run the app, everything works.

The problem

Even if this works, I wonder if there is a way, to have multiple views (of class XibOwnerClass) dragged on a storyboard, and to be able to configure all of them individually trough Interface Builder?

Currently when I drag the UIView and change its custom class to XibOwnerClass I see nothing. I mean, the view is there, and it has its standard properties + inspectable properties. But I can't see triangles defined in xib in this new view. Is there a way to do this?

I am aware that xib is reused in my case (and it is meant to be used like that), so if I change something in a xib, all views that load from it will be affected. But is there a way multiple views to load from the same xib, but when loaded, to see them & setup them individually?

Here is how I load from xib:

-(instancetype)initWithCoder:(NSCoder *)aDecoder{

    if ((self = [super initWithCoder:aDecoder])){

        UIView *myView = [[[NSBundle mainBundle] loadNibNamed:@"MyXib" owner:self options:nil] firstObject];

        if (myView){

            myView.frame = CGRectMake(myView.frame.origin.x, myView.frame.origin.y, self.frame.size.width, self.frame.size.height);
            [self addSubview:myView];

            //Initialize other stuff

            [self commonInit];
        }
    }

    return self;
}

Then in awakeFromNib: I use the values of inspectable properties...

EDIT:

Also I don't have initWithFrame: implemented, but I thought that it is not needed if load from nib (because it is not executed). But maybe this is needed while design time ?

Whirlwind
  • 14,286
  • 11
  • 68
  • 157

2 Answers2

2

can't really say what's wrong when you don't share class codes. but i guess you didnt use prepareForInterfaceBuilder. in your XibOwnerClass override prepareForInterfaceBuilder and call setups for view inside it for example change background colour or so.

override func prepareForInterfaceBuilder() {
        //prepare setups or things to show in storyboard

}
Mohammadalijf
  • 1,387
  • 9
  • 19
  • I will update my question with some code. Also, this method is never executed in my case (I have put a breakpoint). And please note that I see inspectable properties in IB for my XibOwnerClass. I just don't see any content from an actual xib. – Whirlwind Dec 21 '16 at 14:28
  • Ah, this method is called only at design time, not at build time. Anyways, I see no differences. Currently, I don't see nothing. It is just like I have added ordinary view + inspectable properties (in Attributes Inspector). No content can be seen. – Whirlwind Dec 21 '16 at 14:46
  • It worked now, I made an answer as well. Thanks for your answer though. You got my upvote, because that was of course a missing part to solve my issue. – Whirlwind Dec 21 '16 at 15:47
0

Okay, I have solved it. It turned out that I shouldn't use Main Bundle but rather bundle for that class, like described here.

So here is the code:

NSBundle *bundle = [NSBundle bundleForClass:[self class]];

@Mohammadalijf's answer in general is correct. It is just not a direct solution for my issue. But later on, when I fixed bundle thing, I used prepareForInterfaceBuilder like he said, and initialized things there so everything was available at design time.

Some useful stuff related to all this can be found here as well.

Community
  • 1
  • 1
Whirlwind
  • 14,286
  • 11
  • 68
  • 157