views:

423

answers:

3

hi. I messing with iphone developement. I have a uiimageview subclass that I want to use to detect touches. I have sucessfully added to interfacebuilder and I can detect a touch in my UIImageview subclass within my application so all is good on that front. however my UIImageView subclass has a custom initializer which is not called when it is created in interface builder.

if I manually initialize the UIImageview and add it programmatically I think it will work but then I lose the ability to 'see' my positioning in Interface builder.

how can I either

1) 'see' a uiimageview in interface builder that is added in code? (not possible?) 2) call my custom initializer when the subclass is instantiated in interfacebuilder.

thanks

A: 

Can you not simply put your initialisation logic in viewDidLoad? In particular,

-(void)viewDidLoad {
  [super viewDidLoad];
  // Put whatever was in your custom initialiser here.
}
Frank Shearar
Depending on the needs of ninjasmith, this may be the better route. If it's a one time use, your solution is probably ideal. If he's looking for reusability, making a class with the right initializer may be preferable.
Giao
+1  A: 

You likely have put your instructions in the wrong initializer.

According to the documentation, objects unarchived from a NIB are initialized with initWithCode: if they conform to the NSCoding protocol; objects that don't conform to NSCoding are initialized with init.

In this particular case, UIImageView does conform to NSCoding. It's likely that you have you intended for initWithFrame: to be called and put your instructions in that method.

Giao
A: 

Hi thanks for suggestions. I think I'm getting closer to understanding the relationship between the xib and the viewcontroller.

I now am sucessfully adding my UIImageView subclass programmatically and using my custom initiializer which overrides InitWithFrame.

I think I read that the xib calls 'awakeFromNib' so I could equally add my iniitialization code in there. I like the idea of adding it programmatically as I have more control (although harderto set up my IU)

so one more realted question. if I add an UIImageView subclass in interface builder. I can see it and detect touches on it. if I want to refer to it in the view controller class do I have a pointer to it? i.e. is there a variable name for it? the UIImageViews I create myself I obviuosly know what they are called.....

ninjasmith
Rather ask a related question as a new, independent question.
Frank Shearar