Link to home
Start Free TrialLog in
Avatar of carvalhaes
carvalhaes

asked on

How to make an UILabel hidden inside a timer?

Hi.
I'm doing a program where a UILabel should disappear and reappear at intervals of one second. The timer is working fine, but when I put the function "label1000.hidden = Yes" in the void "ChangeVisibility" the UIlabel not disappear. However, if I put this function "label1000.hidden = Yes" in an IBAction of a button, the label disappears when I touch the button on iPhone Simulator (it works).
I saw on another forum that I need setNeedsDisplay function, but I am a beginner and I do not know how to do this.
Here's the code:
.main
#import <UIKit/UIKit.h>
#import "MyViewController.h"
int main(int argc, char *argv[]) {
	MyViewController * start1000 = [[MyViewController alloc] init];
	[start1000 onTimer1000];
	
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}

Open in new window


.h
@interface MyViewController : UIViewController {
    NSTimer *Timer1000;
	
	int Times1000;
	int valor;

	
	UIButton *button1;
	IBOutlet UILabel *label1000;

}
-(void)onTimer1000;

-(void)changeVisibility1000;

@property (nonatomic, retain) IBOutlet UIButton *button1;
@property (nonatomic, retain) IBOutlet UILabel *label1000;


- (IBAction)changeGreeting:(id)sender;
@end

Open in new window


.m
#import "MyViewController.h"

@implementation MyViewController

@synthesize label1000;


-(void)changeVisibility1000 {
	if (Times1000 ==1) {
		Times1000 = 0;
		label1000.hidden = YES;
		printf("a");
	}
	else
	{
		Times1000 = 1;
		label1000.hidden = NO;
		printf("b");
	}

}

-(void)onTimer1000 {
	Timer1000 = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeVisibility1000) userInfo:nil repeats:YES];
}


- (IBAction)changeGreeting:(id)sender {
\\just for test this line bellow		
label1000.hidden = YES;


	if (valor==1) {
		printf("1");
		valor=0;
		[button1 setTitle:@"Stop" forState:UIControlStateNormal];
	}
	else {
		printf("0");
		valor=1;
		[button1 setTitle:@"Start" forState:UIControlStateNormal];
	}
	


  
}


- (void)dealloc {
//   [label release];
//    [string release];
    [super dealloc];
}

Open in new window


The timer works because I see "ababababababa ..." at debug.
Does anyone know the necessary changes in my code?
Sorry for my bad english...
Avatar of SpeedyApocalypse
SpeedyApocalypse
Flag of Canada image

To have intervals at one second as the label hides, just simply do this snippet of code.

(To start the method, call the first method using [self firstHide];)

-(void)viewDidLoad {

[self firstHide];

label.alpha = 1.0;
continue = YES;

}

-(void)firstHide {

label.alpha = 0.0;

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(secondHide) userInfo:nil repeats:YES];

timer = nil;

}

-(void)secondHide {

label.alpha = 1.0;

if(continue == YES) {

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(firstHide) userInfo:nil repeats:YES];

timer = nil;

}

}

As soon as you call firstHide, the label disappears, then, it waits 1 second then goes to secondHide, the label will appear again.  After 1 second, it hides and keeps repeating.  Set the Boolean "continue" to NO and it will stop once the label appears again.
Avatar of carvalhaes
carvalhaes

ASKER

thanks but the code has 3 erros:
(ive changed the 'label' to label1000 and i have declared the 3 void)

1 - at line 'continue = YES;' from void viewdidload the error is
'continue statement not within a loop'

2 - at the same line above, i have the error 'expected ; before = token'

3 - at line 'if(continue == YES) {' from void secondHide, the error is
'expected expression before continue'

thanks for your attention, but could you help me to make this code without errors?
Did you declare the continue as a BOOL in your .h?  

Also, as I am writing this on my PC, I forgot that continue was an actual statement in and of itself.  So change all continue statements with continueBool.  Also declare continueBool in your .h.  

...

I was using label as a placeholder, that is why you had errors.  You had to fill it in with the corresponding label that you had (which you did).

Other than that slight error with the naming on my part, that code will work perfectly fine.

thanks but what i have to write at .h to declare continue as a BOOL?
Declare continueBool in your .h like this:

BOOL continueBool;

Then, in your .m, replace all appearances of continue with continueBool.
If you still want to check your idea with the setNeedsDisplay, add the following line in the end of the changeVisisbility function:
[self setNeedsDisplay:YES];

You can try setNeedsDisplay for the label only:
[label1000 setNeedsDisplay:YES];

thank you! its almost working!
i put a printf("X") at void firstHide and a print("Y") at void secondHide.
in the first second it works well (the label become invisible) but after the second second it becomes to appear and dessapear in a high frequency.
(at debug i can see hundreds of XXXYXYYXYXYXY appearing on each second after the aprox. 4th second of running program)

i didnt count, but i believe that the number of x and y is 2^(second)...

i dont know if i was clear, but how can i fix it?
pgnatyuk,

if i put '[self setNeedsDisplay:YES];' at the end line of changevisibility, the app not even open at iphone simulator.

if i put '[label1000 setNeedsDisplay:YES];' at the end line of changevisibilty, the app opens ok but not happens.

thanks for your attention
Bah, I know why it is appearing and disappearing fast.  Darn PC!  Change the repeats:YES to repeats:NO.  This should make it work perfectly.  Sorry about the random errors :)  I am finally back at my Mac with XCode.
That's what I meant:

-(void)changeVisibility1000 {
        if (Times1000 ==1) {
                Times1000 = 0;
                label1000.hidden = YES;
                printf("a");
        }
        else
        {
                Times1000 = 1;
                label1000.hidden = NO;
                printf("b");
        }
       

        [self setNeedsDisplay:YES];

}

Never mind. If it already works for you - congratulations.
pgnatyuk, thanks, but when i put that your last code, the program dont run.
and speedyapocalypse, thank you!!!
i have more 2 simple questions:
1 - how do i stop this timer (at my ibaction changegreeting)?
2 - what i have to do now, because this is the first time thread that i write here in the expertsexchange? i just click on accept as solution at your name?
i found the solution, i just put continueBool = NO; and it stops with the button.
im just waiting for you, speedapocalypse, tell me if i click accept as solution or accept and award points at your name.
ASKER CERTIFIED SOLUTION
Avatar of SpeedyApocalypse
SpeedyApocalypse
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial