我创建了一个生成随机数的方法,并使用生成的数字更新标签。
- (void)generateNumber {
int i = arc4random() % 100;
NSLog(@"Random Number: %i", i);
NSString *randomNumber = [NSString stringWithFormat:@"%d",i];
self.myLabel.text = randomNumber;}
现在我希望保存该号码,以便在此方法中将旧生成的号码与新号码进行比较:
- (IBAction)downButton {
[self generateNumber];
//And do something like
if(newNumber > oldNumber){
NSLog(@"Number is lower: %i", i); }}
请帮忙怎么做?
答案 0 :(得分:0)
全球声明:
int oldNumber =100;
然后编辑此
- (IBAction)downButton {
int newNumber = [self generateNumber];
//And do something like
if(newNumber > oldNumber){
NSLog(@"Number is lower: %i", newNumber);
}
oldNumber = newNumber;
}
- (int)generateNumber {
int i = arc4random() % 100;
NSLog(@"Random Number: %i", i);
NSString *randomNumber = [NSString stringWithFormat:@"%d",i];
//self.myLabel.text = randomNumber;
return i;
}
答案 1 :(得分:0)
为生成的数字创建一个属性,让你的方法返回新生成的数字。
@property (nonatomic, assign) NSInteger generatedInt;
也许你应该初始化你的财产" generatedInt" ;)
- (NSInteger)generateNumber { ... return i; }
然后在你的downButton方法中比较这两个值:
- (IBAction)downButton {
NSInteger newNumber = [self generateNumber];
if(newNumber < self.generatedInt){
NSLog(@"Number is lower: %i", i);
}
self.generatedInt = newNumber;
}