Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to make an UILabel hidden inside a timer?

Posted on 2010-08-31
13
Medium Priority
?
1,725 Views
Last Modified: 2013-11-25
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...
0
Comment
Question by:carvalhaes
  • 6
  • 5
  • 2
13 Comments
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 33572763
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.
0
 

Author Comment

by:carvalhaes
ID: 33572898
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?
0
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 33572921
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.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:carvalhaes
ID: 33573180
thanks but what i have to write at .h to declare continue as a BOOL?
0
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 33573319
Declare continueBool in your .h like this:

BOOL continueBool;

Then, in your .m, replace all appearances of continue with continueBool.
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33573519
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];

0
 

Author Comment

by:carvalhaes
ID: 33573532
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?
0
 

Author Comment

by:carvalhaes
ID: 33573565
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
0
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 33573588
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.
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33573637
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.
0
 

Author Comment

by:carvalhaes
ID: 33573673
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?
0
 

Author Comment

by:carvalhaes
ID: 33573722
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.
0
 
LVL 13

Accepted Solution

by:
SpeedyApocalypse earned 2000 total points
ID: 33577978
You can accept as a solution if you feel that the question has been answered.  You will then be able to award points. :-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question