基本上,我试图构建一些代码,允许NSNumber每分钟减少1次,
这就是我到目前为止所拥有的。
- (void)viewDidLoad {
[super viewDidLoad];
NSNumber *numVar = [NSNumber numberWithUnsignedChar:99];
[self num];
}
-(void)num{
usleep(60000000);
NSNumber *numVar = [NSNumber numberWithUnsignedChar:numVar-1];
[self num];
}首先,它不会每分钟减少一个,其次,在viewDidLoad中使用初始viewDidLoad是否错误?
发布于 2014-10-03 11:56:55
我们尝试了这种方法,但是它对我们所需要的工作很有效:您需要在代码中重新安排这些内容。
- (void)viewDidLoad{
NSTimer *timer;
int minutes = 0;
//Creating a label that shows the timer ; i assume you have one that is already linked
_labelTimer.text = [NSString stringWithFormat:@"This is %i minute", minutes];
//Creating the timer itself with the NSTimer object
timer = [NSTimer scheduledTimerWithTimeInterval: 60.0 target: self selector:@selector(decreaseTimeCount) userInfo:nil repeats:YES];
//Note that 60 is the number of seconds before it calls the selector. You can use 1 if you want it to change your timer/clock every second
}
- (void)decreaseTimeCount{
//The method changes the value of my variable and updates the label. If i'm at the 10th minute, i could do something, like perform a segue.
minute+=1;
_labTimer.text = [NSString stringWithFormat:@"This is %i minutes", minutes];
if (minutes == 10){
//do stuff
}
}发布于 2014-10-03 11:44:03
你睡在主线程中,阻塞你的UI。您需要使用NSTimer或类似的定时机制来周期性地运行该函数,而不需要阻塞。
发布于 2014-10-03 12:07:37
@implementation YourViewController
{
NSNumber *numVar;
}
- (void)viewDidLoad
{
[super viewDidLoad];
numVar = [NSNumber numberWithUnsignedChar:99];
[NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
}
- (void)timerFireMethod:(NSTimer *)timer
{
numVar = [NSNumber numberWithInt:[numVar intValue] - 1];
}https://stackoverflow.com/questions/26178246
复制相似问题