0

I am trying to figure out all the different ways to create a view and am having problems with dragging it out from the object library when I have a custom xib.

This is what I have in my UIView:

-(id)initWithCoder:(NSCoder *)aDecoder{
  self=[super initWithCoder:aDecoder];
    if (self) {
    }
    NSLog(@"about to return here");
    return self;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       UIView *myView=[[[NSBundle mainBundle] loadNibNamed:@"testview" owner:nil options:nil] lastObject];
      [self addSubview:myView];
    }
    return self;
}

// drawRect is commented out

I am able to drag out a custom view if I override drawRect but am unable to do this if I drag it out from the Object Library and update the Custom Class in the Identity Inspector.

I know I am doing something really naive but I can't believe I can't get this to work. Any ideas on how to be able to drag out a generic view to a storyboard controller and update the Custom Class and have it work?

edit #1

here's a screenshot. It is trying to draw something but it's like it doesn't know what to draw. I've added a couple of setNeedsDisplay but with no success. The main view's background is light blue and the custom view should be green. The white lines up with where this custom view should be.

enter image description here

timpone
  • 19,235
  • 36
  • 121
  • 211

1 Answers1

0

Even with the current version of XCode 4.6.3, you cannot have custom objects shown in Interface Builder without writing a XCode Plugin (which is not even supported at the moment).

So the only solution is to drag in a Generic View from the existing objects, and change the object type afterwards. Unfortunately, you won't see any visuals of your custom UIView in Interface Builder. Annoying, isn't it?

Rob van der Veer
  • 1,148
  • 1
  • 7
  • 20
  • agreed, it's annoying. Yeah, I understand what you said - but that is only working when I override drawRect. The problem I'm having is that if I use an external nib and drag it onto a storyboard, it doesn't load the nib. Doing it in drawRect works fine though. – timpone Jul 10 '13 at 18:19
  • Are you sure the external nib is bundled with the main app? I'm sorry I can't help you any further... – Rob van der Veer Jul 10 '13 at 18:24
  • 1
    Usually I get bundle errors on renames but am able to load the nibs above. I feel like I need to do a loadNibNamed in the initWithCoder ... – timpone Jul 10 '13 at 18:29
  • the problem is that initWithCoder calls initWithCoder leading to a recursive call – timpone Jul 10 '13 at 20:14
  • see that there is sol'n here regarding subViews (not the selected answer) http://stackoverflow.com/questions/11450258/infinite-loop-when-overriding-initwithcoder – timpone Jul 10 '13 at 20:26