Solved

How to make an UILabel hidden inside a timer?

Posted on 2010-08-31
13
1,696 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now