ios - 自动减少自身的NSNumber-1

标签 ios sleep void nsnumber viewdidload

好的,基本上,我正在尝试构建一些代码,以使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/

相关文章:

C:从 void* 读取 int**

ios - 在没有 Xcode 的情况下添加 Fabric 应用程序 (Twitter)

ios - 尝试在我的个人 iPhone 上模拟我的 Xcode 项目

长时间最小化后Android开始 Activity

java - 为什么 InterruptedException 仅从阻塞方法(如 sleep() 和 wait())中抛出?

c - 父进程不会使用 signal() 等待所有子进程退出

c - 指向数组的指针 - C 编程

c - 空指针的奇怪行为

ios - 如何在可见的 Scrollview 中获取 Button 位置?

ios - 如何使用 AWS iOS SDK v2 将 UIImage 上传到 S3