Changing a UILabel after a specific time

爷,独闯天下 提交于 2019-12-11 18:12:54

问题


Im a bit stuck and need some ideas.

I want to make an app where it counts up from 0-12 then another UILabel changes to 1. The first label starts again from 0-12 then the label changes to 2. So on...

I have tried a few ways adding in a NSTimer scheduledTimer and making a few selectors it worked to a certain extent but not to my liking.

I don't need any code examples (although would be nice) but just some ideas would be nice thanks. :)


回答1:


do you want to change label according to time or by press button?

So you want to change label by timer so here it is.

Fist

#define TimeIntervelInSec 1.0
#define countMaxLimitForLower 12

Make 2 int in your .h file

int countLower;
int countHigher;

IBOutlet UILabel *lblLowerCount;
IBOutlet UILabel *lblHigherCount;

and in put this is in .m

- (void)viewDidLoad
{
    [super viewDidLoad];

   countLower = 0;
   countHigher = 0;

    [NSTimer scheduledTimerWithTimeInterval:TimeIntervelInSec
                                     target:self
                                   selector:@selector(timerChanged)
                                   userInfo:nil
                                    repeats:YES ];
}

-(void)timerChanged
{
    if (countLower >= countMaxLimitForLower)
    {
        countHigher++;
        countLower = 0;
    }
    else
    {
        countLower ++;
    }
    lblLowerCount.text = [NSString stringWithFormat:@"%d",countLower];
    lblHigherCount.text = [NSString stringWithFormat:@"%d",countHigher];

}



回答2:


@YashpalJavia was on the right track, but there are several issues with the proposed code:

  • Method names should start with lower case
  • The timer code won't do what the OP asked for
  • Timer methods should take the timer as a parameter

Starting from that code:

Create an instance variable count, which will count total seconds since the timer is started.

- (void) startTimer;
{
    count = 0;

    [NSTimer scheduledTimerWithTimeInterval:TimeIntervelInSec
                                     target:self
                                   selector:@selector(timerChanged:)
                                   userInfo:nil
                                    repeats:YES ];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self startTimer];
}

-(void)timerChanged: (NSTimer *) timer;
{
    const int units = 12;
    count++;
    int countLower;
    int countHigher;
    countLower = count % units; //Use the modulo operator to get a value from 0 - 11
    countUpper = countLower / units;
    countLowerLabel.text = [NSString stringWithFormat: @"%d", countLower);
    countUpperLabel.text = [NSString stringWithFormat: @"%d", countUpper);
}

The code above will make the lower value go from 0 to 11, then increment the higher value and reset the lower value back to 0. If you really want the lower to go from 0 to 12 (13 possible values) then change units to 13, but I bet that's not what you want.




回答3:


If you counter increase in per second you can try something like this.

int count;
count=0;
[self performSelector:@selector(updateMyLabel:) withObject:nil afterDelay:12.0];

-(void)updateMyLabel:(id)sender
 {
    count++;
    NSString *counterString = [NSString stringWithFormat:@"%d", count];
    secondLabel.text=counterString;
 }

I hope it will work for you



来源:https://stackoverflow.com/questions/22281691/changing-a-uilabel-after-a-specific-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!