views:

301

answers:

3

Hi !

I'm trying to change the text of an UILabel with a little transition (fade out, change text, fade in) but I'm facing some problems. Here is my code:

- (void) setTextWithFade:(NSString *)text {
    [self setAlpha:1];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.25];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(fadeDidStop:finished:context:)];
    [self setAlpha:0];
    [UIView commitAnimations];
}

- (void)fadeDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.25];
    [self setAlpha:1];
    [UIView commitAnimations];
}

This code "works" (the effect is working well), but what I can't figure out is how to change the label text in the "fadeDidStop" function... How can I "pass" the text variable from my first function to the second?

Thanks in advance

A: 

maybe the simpliest way is to declare NSString object in your .h file and use it to store text to change.

Morion
I think I will do that but I just want to know if there is a better way to do it.
Alexandre
+2  A: 

You pass the text in the context:

[UIView beginAnimations:nil context:text];

Then in your fadeDidStop method:

NSString *text = (NSString*) context;

Be mindful when passing objects in the context, make sure they are retained properly.

lyonanderson
Correct, except you might want to actually set your context to something other than nil :)
rein
Thanks, it did the trick!
Alexandre
Good call rein. My bad, it was a late night ;-)
lyonanderson
A: 
...   
 [UIView beginAnimations:nil context:[text retain]];
...


- (void)fadeDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    self.text = (NSStrinhg *)context;
    [context release];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.25];
    [self setAlpha:1];
    [UIView commitAnimations];
}
wkw