CyReX
CyReX

Reputation: 1576

Updating UILabel text with NSTimer programmatically

I am trying to implement method that affect UILabel text each every 1 seconds , It is going to be a reminder text like " You can skip ad in 5,4,3,2,1 skip now >>"

My method works fine except in 2nd and 3rd seconds timer call method like a more than 1 times each on second. I am using UIApplication sharedapplication.keywindows for present UI over full screen MPMoviePlayerController . Here is my method :

-(void)closeButtonForVideoPlayer
{

    myButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-30, 12, 25, 25)];

    window = [UIApplication sharedApplication].keyWindow;

    if (!window)
    {
        window = [[UIApplication sharedApplication].windows objectAtIndex:0];
    }

    [myButton addTarget:self action:@selector(videoFinished:) forControlEvents:UIControlEventTouchUpInside];

    [myButton setImage:self.closeButton forState:UIControlStateNormal];

    labelReminder = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width-130, 12, 130, 35)];

    labelReminder.textColor = [UIColor whiteColor];

    labelReminder.backgroundColor = [UIColor blackColor];

    self.timderReminder = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                       target: self
                                                         selector:@selector(reminderForVideoPlayer:)
                                                         userInfo: @{@"boolType":@"YES"} repeats:NO];


}

The reminder method :

-(void)reminderForVideoPlayer:(NSTimer *)userInfo
{

    NSString *isVideoAdvertisement = [userInfo.userInfo objectForKey:@"boolType"];

    NSLog(@"NSTimer info = %@",userInfo.userInfo);


    if([isVideoAdvertisement isEqualToString:@"YES"])
    {
        if(remind >= 5)
        {
            [self.timderReminder invalidate];

            remind = 0;

            NSLog(@"reklam geçildi window");

            [labelReminder removeFromSuperview];

            labelReminder = nil;

            [labelReminder setHidden:YES];

            [window addSubview:myButton];


        } else {

            [window addSubview:labelReminder];

            [labelReminder setText:[NSString stringWithFormat:@"Reklami geç: %d",5-remind]];

            NSLog(@"reminder window = %d",remind);

            remind ++;

            self.timderReminder = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                                   target: self
                                                                 selector:@selector(reminderForVideoPlayer:)
                                                                 userInfo: @{@"boolType":@"YES"} repeats:NO];

        }
}

Which part i am making mistake ? and how i can fix that issue ?

Best Regards,

Onder OZCAN

Upvotes: 0

Views: 335

Answers (3)

Mrunal
Mrunal

Reputation: 14118

in viewDidLoad

self.timderReminder = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                   target: self
                                                     selector:@selector(reminderForVideoPlayer:)
                                                     userInfo: @{@"boolType":@"YES"} repeats:YES];

and

-(void)reminderForVideoPlayer:(NSTimer *)userInfo
{
    NSString *isVideoAdvertisement = [userInfo.userInfo objectForKey:@"boolType"];
    NSLog(@"NSTimer info = %@",userInfo.userInfo);

    if([isVideoAdvertisement isEqualToString:@"YES"])
    {
        if(remind < 5)
        {
            [labelReminder setText:[NSString stringWithFormat:@"Reklami geç: %d",5-remind]];
            NSLog(@"reminder window = %d",remind);

            [labelReminder setHidden:NO]; 
        } else {
            [labelReminder setHidden:YES];

            // [window addSubview:myButton]; //add this button in view did load only over here use setHidden method
            [myButton setHidden: NO];
        }
}

Try with this code.

Upvotes: 1

ugur
ugur

Reputation: 842

Change the repeats parameter of NSTimer to YES. And you won't have to create a timer again each time it's fired. When your counter reaches to 5, just invalidate your timer.

Upvotes: 0

Jay Gajjar
Jay Gajjar

Reputation: 2741

Try this control it will fulfil your requirements

https://github.com/mineschan/MZTimerLabel

Upvotes: 1

Related Questions