0

I want to show user that the value initially is 0 than it increase to 1 than 2 than 3 sequence repeat after some seconds

Worked done by me I write this code in viewdidload but it print 99 in label directly but i want to show user changing values from 0 to 99.

for(int i=0;i<100;i++){
        _totalEffort.text=[NSString stringWithFormat:@"%d",i];
}
kb920
  • 3,039
  • 2
  • 33
  • 44
JeeVan TiWari
  • 325
  • 2
  • 15
  • 2
    Use NSTimer and update the value in label after some interval of time. – kb920 Jan 31 '17 at 11:47
  • for(int i=0;i<100;i++){ [NSTimer scheduledTimerWithTimeInterval:1000 target:self selector:@selector(self) userInfo:nil repeats:NO]; _totalEffort.text=[NSString stringWithFormat:@"%d",i]; } **Not Worked for me ** – JeeVan TiWari Jan 31 '17 at 11:52
  • Please refer http://stackoverflow.com/questions/17731840/how-to-add-a-delay-to-a-loop – Emel Elias Jan 31 '17 at 11:53

5 Answers5

2

I think this will help you:

 @interface ViewController (){
        int i;
        NSTimer *myTimer;
 }

 @end

 @implementation ViewController

 - (void)viewDidLoad {
     [super viewDidLoad];
     myTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
                                         target:self
                                       selector:@selector(setData)
                                       userInfo:nil
                                        repeats:YES];

       i = 0;

  }

  -(void)setData{
       _totalEffort.text=[NSString stringWithFormat:@"%d",i];
        i = i + 1;
        if(i == 100){
            [myTimer invalidate];
            myTimer = nil;
        }
    }
CodeChanger
  • 7,953
  • 5
  • 49
  • 80
Vandana pansuria
  • 764
  • 6
  • 14
0

Based on your requirement you need to use NSTimer for update your label progress

Step 1: Create 2 variable in your @interface class

@interface BUViewController: UIViewController {
    NSTimer *timerCount;
    int progress;
}

Step 2: Start Timer in your viewDidLoad() with progress value 0.

progress = 0;
timerCount = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];

Step 3: create method with timer update label text an check progress value in it and stop timer while its reach 100.

-(void)updateLable{

    if (progress<100) {
        lblProgress.text = [NSString stringWithFormat:@"%d",progress];
        progress++;
    }else{
        [timerCount invalidate];
        timerCount = nil;
    }

}

Hope this will help you to achieve your expected output.

CodeChanger
  • 7,953
  • 5
  • 49
  • 80
0

For this you can use NSTimer you need to update UILabel value in its selector method. You can use this code.

-(void)viewDidLoad{
  [super viewDidLoad];
  self.time = 0;
  _labelTimer.text =@"0";
  [self startTimer];
}
- (void)startTimer {
   self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
   [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
   }

- (void)timerAction:(NSTimer *)timer {
    self.time++;
    if (self.time == 100)
     {
        [self stopTimer];
     }
       _labelTimer.text = [NSString stringWithFormat:@"%d",self.time];

 }

 -(void)stopTimer{
    [self.timer invalidate];
    self.timer = nil;
}
Sunny
  • 821
  • 6
  • 17
0

Here is code for your problem .hope it helps you.

- (void)viewDidLoad 
{

 [super viewDidLoad];

    counter = 0;

    t =  [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];

}

-(void)updateLabel
{

    for (int i = counter; i<counter+1; i++)
    {

        _totalEffort.text=[NSString stringWithFormat:@"%d",i];
    }
    counter++;

    if (counter ==101)
    {
        [t invalidate];
    }

}
Yatish Agrawal
  • 452
  • 5
  • 15
Hetal
  • 51
  • 8
0

You are blocking run loop with your cycle. You just need resume it in each iteration.

for(int i=0;i<100;i++){
        _totalEffort.text=@(i).stringValue;
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];

}
Cy-4AH
  • 4,370
  • 2
  • 15
  • 22