1

I'm trying to change though a timer in a different method the text of a label I created programmatically in a UIView overlay that goes on a UIImagePickerviewController, but of course when I try to change the text in this way

labelname.text = @"TEST";

I get the error "use of undeclared identifier labelname"

How can I refer to that specific label? Should I create a new label each time the timer ticks?

I tried to declare it in the .h file, but I'm guessing i'm just creating a different label...any ideas?

overkill
  • 103
  • 2
  • 8
  • You need to declare a `UILabel` class variable `labelname` and initialize it in `viewDidLoad` method so you can call it now `self.labelname.text=@"TEST"`. – Zigii Wong Feb 24 '15 at 00:20
  • Unfortunately that doesn't help :( still get the same error... – overkill Feb 24 '15 at 08:38

1 Answers1

0

Just Pass the Label As a whole to the Timer Function:

Say Your Label is defined in ViewDidLoad like this :

- (void)viewDidLoad 
{
     //Do Something here
     UILabel  * label = [[UILabel alloc] 
     initWithFrame:CGRectMake(xcoordinateLabel, ycoordinateLabel, width, height)];
     label.backgroundColor = [UIColor clearColor];
     label.textAlignment = UITextAlignmentCenter;
     label.textColor=[UIColor whiteColor];
     label.text = @"TEST";
     [self.view addSubview:label];
     [self changeLabelText:label];
 }

Where your changeLabelText is defined as this with some delay:

- (void) changeLabelText:(UILabel *)label
{
    //Changing Values after 10s Delay
    double delayInSeconds = 10.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 
    (int64_t)(delayInSeconds * NSEC_PER_SEC));

    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    label.text = @"TESTING Change";
    });
}

Your Timer function can replace changeLabelText

Ducktales
  • 312
  • 4
  • 11
  • I thought this was the right way to do it (at least it didn't give any errors when compiling). The label is still declared in a different method, but now i replaced the timer with this -(void)timerCount:(UILabel *)label{} but now when I'm calling the timer through timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCount) userInfo: nil repeats:YES]; I get an error because i'm not passing the label as a parameter. replacing timerCount with timerCount:label doesn't help :( – overkill Feb 24 '15 at 08:55
  • You can pass the label argument in the UserInfo and then retrieve it in the Timer function, Pass like this NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 2.0 target: self selector:@selector(timerCount:) userInfo: label repeats:YES]; and retrieve like - (void) timerCount:(NSTimer *) Timer { UILabel * label = [Timer userInfo]; – Ducktales Feb 24 '15 at 20:45
  • You mean replace -(void)timer with - (void) timerCount:(NSTimer *) Timer { UILabel * label = [Timer userInfo] ? Where's the closing curly? Im so lost :'( – overkill Feb 24 '15 at 22:04
  • Yes - (void) timerCount:(NSTimer *) Timer { UILabel * label = [Timer userInfo]; //Do what you want with the label } – Ducktales Feb 25 '15 at 00:51
  • OH MY GOD IT WORKS!!! I still have no idea how, but I'm thanking you already! could you explain what's going on there? I understand that we're calling the method and passing a parameter, but what I don't follow is that the methoud we declare another timer Timer -> -(void) timerCount:(NSTimer *) Timer – overkill Feb 25 '15 at 08:47
  • There is only one timer function -> -(void) timerCount:(NSTimer *) Timer which takes in the NSTimer as an argument , what we are doing is just wrapping the Label object inside the Timer object and passing it. Later the Label object is being retrieved from inside using this line UILabel * label = [Timer userInfo]; – Ducktales Feb 25 '15 at 09:23