rudighert
rudighert

Reputation: 315

variable value in different methods

my problem is very simple (sorry my english, im speak spanish)

i have

NSString *ClaseUsuario;

AND

@property (strong, nonatomic) NSString *ClaseUsuario;

I have a method that changes the value of my variable

-(IBAction)setLabelEdad{
int val=(int)[botonEdad value];
if(val>0 && val<22){ClaseUsuario=@"C";}
if(val>=22 && val<35){ClaseUsuario=@"B";}
if(val>=35){ClaseUsuario=@"A";}

NSLog(ClaseUsuario);

}

but my problem is that when I call my variable from another method the answer is NULL

-(IBAction)VerOfertas{
NSLog(@"%@%@",@"Ofertas usario tipo: ",ClaseUsuario);   
}

I need to save the value assigned by the above method, please help! and thank you very much

Upvotes: 0

Views: 51

Answers (1)

Rob Napier
Rob Napier

Reputation: 299325

There are several things to fix here:

Use a leading lowercase letter for your property: claseUsuario. This matters for KVC.

Do not declare an ivar at all. Just use

@syntheisze claseUsuario=_claseUsuario;

(Note the leading underscore. There is seldom reason to explicitly declare an instance variable any more unless you're writing for very old systems.)

Except in init and dealloc, always use the accessor. So this should be self.claseUsuario = @"C";. This will ensure that memory management works correctly.

Generally your NSLog should be of this form:

NSLog(@"Ofertas usario tipo: %@", self.claseUsuario);

Do not name your IBAction with a leading set. This can confuse KVC because it looks like an accessor.

The likely actual cause of your problem is that either setLabelEdad is never called, or you clear claseUsuario somewhere else, or you are looking at the wrong object. Put a breakpoint in setLabelEdad to test these.

Upvotes: 1

Related Questions