iOS延迟再次发生的动作

时间:2012-07-21 23:52:29

标签: ios

所以我在带有arc4random的forumla中构建了一个随机数生成器。 所以forumla有3个变量

Variable 1 > User enters it
Variable 2 > User enters it
Variable 3 > arc4random

我的问题是,如果变量1和2相同,我怎么能告诉arc4random创建相同的数字

So if
V1 = 5
V2 = 4
V3 = 68 and the user enters 5&4 it shall create 68 again, BUT after 10 minutes it may use another random number

Sry不得不把它作为代码,因为它不会让我上传这个问题,否则

1 个答案:

答案 0 :(得分:0)

这就是我所理解的:你想要求arc4random生成2个数字和另一个数字,但如果用户在接下来的10分钟内引入相同的2个数字,则arc4random数字必须相等。

我将尝试将数字保存在其他变量中(例如saveV1,saveV2和saveV3)并以600秒的时间间隔启动NSTimer。 NSTimer的选择器将清除saveVX变量。所以这就是结构:

    1. Ask V1 
    2. Ask V2
    3. Check if there are saved numbers and check if they are V1 == saveV1 and V2 == saveV2
    4A. If they are the same: V3 = saveV3 
    4B. If they aren't: Generate V3
    5. Save V1, V2, V3 in saveV1, saveV2 and saveV3.

这是你要求的吗?

EDITED: 这是代码:

//v1, v2, v3, saveV1, saveV2, saveV3 declared in the .h file
-(void) function
{
  v1 = [[v1TextField text] intValue];
  v2 = [[v2TextField text] intValue];

if(v1 == saveV1 && v2 == saveV2) 
{
  v3 = saveV3;
}
else
{
  v3 = arc4random()%100; //This line generates a random number between 0 and 99.
  [NSTimer scheduledTimerWithTimeInterval:600.0f target:self selector:@selector(deleteVs)   userInfo:nil repeats:NO];
}

saveV1 = v1;
saveV2 = v2;
saveV3 = v3;

NSLog(@"V1: %d, V2: %d, V3: %d",v1,v2,v3);

}

-(void)deleteVs
{
saveV1 = 0;
saveV2 = 0;
saveV3 = 0;
//You can change this and put -1 to have 0 as a valid value of v1 and v2.
}
相关问题