user2771150
user2771150

Reputation: 722

How to hide Text from UILabel in IOS

I'm trying to display a set of numbers on a UILabel sequentially.

For example: I have an array with 5 numbers and each time I want one number from the array to be displayed for 2 seconds on the screen then disappear and then the next number…

This is what i have done so far, but it is not quite what I want. The code below just displays the last number in the array for 2 seconds and then disappears.

 - (void)viewDidLoad
{
     [super viewDidLoad];      

     NSMutableArray* myArray = [NSMutableArray array];
     for (int i=0; i<5; i++) {
            NSNumber *temp;
            temp = [myArray objectAtIndex:i];
            [self displayNumber:temp];

            [myLabel setHidden:NO];
    }
}

-(void) displayNumber: (NSNumber *) myNumber{
    myLabel.text = [NSString stringWithFormat:@"%@", myNumber];
    NSTimer *myTimer;
    myTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(hideLabel) userInfo:Nil repeats:NO];

}

-(void) hideLabel{
    [myLabel setHidden:YES];
}

I thought about having 5 functions and each gets called sequentially after 2 seconds, but that is very inefficient.

Are there any other ways I can do this?

Upvotes: 1

Views: 1038

Answers (5)

Shankar BS
Shankar BS

Reputation: 8402

try this hope this helps u .. :)

  - (void)viewDidLoad 
   { 
     [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
     [super viewDidLoad];

      myArray = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithInt:3],[NSNumber numberWithInt:5],[NSNumber numberWithInt:6],[NSNumber numberWithInt:7],[NSNumber numberWithInt:10], nil]; //your array contains any number you want

     index = 0; //showing the numbers from 0th index to last indx
     [self.myLabel setHidden:NO];

     [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(DisplayFortwoSec) userInfo:nil repeats:YES];//call the method reguraly for 2 sec
   }

   - (void)DisplayFortwoSec
   {
      [self.myLabel setHidden:YES];
       if(index == ([myArray count] - 1))
        index = 0;//other stuffs to come out of loop like invalidating the timer
       [self.myLabel setHidden:NO];
       NSNumber *num = [myArray objectAtIndex:index];
       NSString *strNum = [num stringValue];
       self.myLabel.text = strNum;
       [self.myLabel setHidden:NO];
       index ++;

   }

Upvotes: 0

Siddh
Siddh

Reputation: 712

Change this method

Hope it will works for you what you want

1) Define

NSMutableArray* myArray , int repeatCount; and NSTimer *myTimer in .h file and set its property;

2)

-(void)viewDidLoad
{
     [super viewDidLoad];     
     //This will initialize your timer
     myTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showLabel) userInfo:Nil repeats:NO];
     //This will trigger your timer 
     [myTimer fire];
     //Your timer will run in a loop
     [[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];   

     myArray = [NSMutableArray array];
}

-(void) displayNumber: (NSNumber *) myNumber{
     myLabel.text = [NSString stringWithFormat:@"%@", myNumber];
}

-(void) showLabel{        
     if(repeatCount >= [myArray count])
     {
         repeatCount = 0;
     }
     NSNumber *temp = [myArray objectAtIndex:repeatCount];
     repeatCount ++;
     [self displayNumber:temp];   
     [myLabel setHidden:NO];
}

Upvotes: 1

Gad
Gad

Reputation: 2877

Try this:

// Initialize the timer when you want to change the first number
[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(changeNumber:) userInfo:@{@"currentNumber" : [NSNumber numberWithInt:0]} repeats:NO];

-(void)changeNumber:(NSTimer *)timer
{
    NSDictionary * dictionary = [timer userInfo];
    int number = [(NSNumber *)[dictionary objectForKey:@"currentNumber"] integerValue];
    if (number < [numbersArray count])
    {
        myLabel.text = [NSString stringWithFormat:@"%@", [numbersArray objectAtIndex:number]];
        [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(changeNumber:) userInfo:@{@"currentNumber" : [NSNumber numberWithInt:number + 1]} repeats:NO];
    }
}

Upvotes: 0

Adithya
Adithya

Reputation: 4705

Pass the array with NSNumber instances to the below method

- (void)updateLabelWithArray:(NSMutableArray *)array
{
    if ([array count] != 0)
    {
        myLabel.text = [NSString stringWithFormat:@"%@", [array firstObject]];
        [array removeObjectAtIndex:0];
        [self performSelector:@selector(updateLabelWithArray:)
                   withObject:array
                   afterDelay:2.0f];
    }
}

This should do.

Upvotes: 0

kaspartus
kaspartus

Reputation: 1365

I think, that you can use timer with counter.

Create 3 properties:

@property (nonatomic, assign) int counter;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, strong) NSTimer *timer;

Set them in viewDidLoad: and start timer(with repetition):

- (void)viewDidLoad {
    [super viewDidLoad];
    _counter = 0;
    _array = @[@0, @1, @2, @3, @4, @5];
    _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextLabel) userInfo:Nil repeats:YES];
}

Update label:

- (void)nextLabel{
    if (_counter == [_array count]) {
        //stop in the end of array
        [_timer invalidate];
        return;
    }
    [_label setText:[NSString stringWithFormat:@"%@", _array[_counter]]];
    _counter++;
}

Upvotes: 0

Related Questions