好的,基本上,我正在尝试构建一些代码,以使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];
}
首先,它不会每分钟减少一次;其次,将初始
numVar
包含在viewDidLoad
中是错误的吗?
最佳答案
我们尝试了这个,它可以很好地满足我们的需求:
您需要在代码中重新安排
- (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
}
}
关于ios - 自动减少自身的NSNumber-1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26178246/