[英]Changing a UILabel after a specific time
我有點卡住,需要一些想法。
我想制作一個應用,其計數從0-12開始,然后另一個UILabel變為1。第一個標簽再次從0-12開始,然后標簽變為2。依此類推...
我嘗試了幾種添加NSTimercheduledTimer的方法,並使一些選擇器在一定程度上可以正常工作,但我並不喜歡。
我不需要任何代碼示例(盡管會很好),但是僅需一些想法就可以了。 :)
您要根據時間或按按鈕更改標簽嗎?
因此,您想按計時器更改標簽,就在這里。
拳頭
#define TimeIntervelInSec 1.0
#define countMaxLimitForLower 12
在您的.h文件中添加2 int
int countLower;
int countHigher;
IBOutlet UILabel *lblLowerCount;
IBOutlet UILabel *lblHigherCount;
並放在.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];
}
@YashpalJavia處在正確的軌道上,但是建議的代碼存在多個問題:
從該代碼開始:
創建一個實例變量計數,它將計數自計時器啟動以來的總秒數。
- (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);
}
上面的代碼將使較低的值從0變為11,然后遞增較高的值並將較低的值重置為0。如果您確實希望較低的值從0變為12(13個可能的值),則將單位更改為13 ,但我敢打賭這不是您想要的。
如果您反對每秒增加,您可以嘗試這樣的事情。
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;
}
我希望它對你有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.