SantiagoRodriguez
SantiagoRodriguez

Reputation: 308

Hidding UILabel on Init

I'm having a problem with this basic code:

-(id)init{
self = [super init];
if(self){
    self.mensaje = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 100.0, 100.0, 100.0)];
    [self.mensaje setText:@"He vuelto"];

    [self.view addSubview:self.mensaje];
    [self.mensaje setHidden:YES];
}
return self;
}

All the code works fine, except [self.mensaje setHidden:YES];. The Label is always shown at start.

I hope could help me, this is basic, but necessary!!

Good luck!

Upvotes: 0

Views: 233

Answers (1)

XJones
XJones

Reputation: 21967

This code is in the wrong place. You shouldn't be creating and working with views in the initializer of a view controller (assuming the above code is inside a view controller class).

instead, do the following:

- (id)init
{
    self = [super init];
    if (self) {
        // init any state other than views
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.mensaje = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 100.0, 100.0, 100.0)];
    [self.mensaje setText:@"He vuelto"];
    [self.view addSubview:self.mensaje];
    [self.mensaje setHidden:YES];
}

This also assumes you are using ARC. If not, you need to add autorelease as follows:

    self.mensaje = [[[UILabel alloc] initWithFrame:CGRectMake(100.0, 100.0, 100.0, 100.0)] autorelease];

Upvotes: 4

Related Questions