views:

232

answers:

1

Hi

I wan' t to implement a custo initialization method for my UIViewController Subclass to "replace" the initWithNibName method.

This is the code:

- (id) initWithMessage:(NSString *)message
{

    if ((self = [super initWithNibName:@"ToolTip" bundle:nil])) 
    {

     label.text = message;

    }

     return self;

}

The Label isloadedo from Xib but at this point the reference to the Label is nil (probably because the xib is not loaded yet?). Can anyone know a solution for that? thanks

A: 

You should declare the label programmatically and initialize it within the init rather than do it from the nib.

This is how :

Assume UILabel *label is a class variable with @property and @synthesize defined for it.

- (id) initWithMessage:(NSString *)message {

if ((self = [super initWithNibName:@"ToolTip" bundle:nil])) 
{
  label = [[UILabel alloc] init];

  label.text = message;
  [self.view addSubView:label];

}

 return self;

}

Release the label in the "dealloc" method. Hope this helps.

Aditi