1

I have a very simple UIViewController subclass. I've set an image view as its view. It takes the entire window.When I create a label and add it like this

 UILabel *titleLabel=[self labelWithText:@"Some text"];
 [self.imageView addSubview: titleLabel];

the label does not appear. But if I add it like this

 [self.view addSubview: titleLabel];

it works, I see the label added to the view. A UIImageView is a subclass of UIView and it responds to the selector addSubview:. Then why does not this work?

Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206
  • 1
    "I've set an image view as its view" - can you take a quick screenshot of the view hierarchy in your xib and post it please? – Damo Mar 02 '12 at 10:57
  • 2
    If your imageView is the mainview you should be accessing it via `self.view`. My guess is that your imageView instance outlet is not connected properly and it is set to nil when you try to add the UILabel to it. – Rog Mar 02 '12 at 10:59
  • @Damo, unfortunately I'm not at my Mac right now, but if you want I can send you the storyboard. – Mikayil Abdullayev Mar 02 '12 at 11:04
  • @Rog, I never explicitly set the imageView property to nil. I set the image propery and can see the image. As the image takes only the top half part of the window I add an embedded youtube video just below the image. So if the image view was set to nil I would not be able to see the image, right? – Mikayil Abdullayev Mar 02 '12 at 11:08
  • What I was getting at is - have you deleted the view from your nib altogether? BUT I think Rog has the right of it - i.e. if you have linked the image view up as the 'view for that nib' - then you access it as self.view – Damo Mar 02 '12 at 11:09
  • It's a duplicate of [this question](http://stackoverflow.com/questions/2415561/apple-interface-builder-adding-subview-to-uiimageview). You just can't add a subview to UIImageView, period. – ayoy Mar 02 '12 at 11:12
  • Hmm, that point sounds right to me.I did remove the UIView object from the nib and put a UIImageView object instead. Every viewcontroller should have a main view. If the view I'm refering to is the direct view of the controller then it seems I should call it as self.view. But anyway, why shold not I be able to add a subview to any view? – Mikayil Abdullayev Mar 02 '12 at 11:13

0 Answers0