我需要一些编程逻辑方面的帮助......我需要循环使用此方法在我的标签中显示数学问题,然后再sleep(5)
然后再循环。我试过的任何东西都冻结了程序。请帮忙!我一直在尝试我所知道的所有东西!
编辑:我编写了代码到这个,3秒后它消失了显示问题的标签但随后它崩溃了,调试器显示2010-08-06 10:43:27.776 Reactor [13444:207] modifying layer that is being finalized - 0x5c13500
//quickfire problems
-(void)simpleMath{ //"Tap when the answer is X"
isTimeToTap = NO;
int problemSelector = arc4random() % 4;
NSArray *mathProblems = [[NSArray alloc] initWithObjects:@"6 - 1",@"2 + 3",@"3 x 2",@"3 x 1",@"2 x 4",nil]; //correct index 2
NSArray *mathAnswers = [[NSArray alloc] initWithObjects:@"5",@"5",@"6",@"3",@"8",nil]; //correct index 2
if ([mathAnswers objectAtIndex:problemSelector] == @"6") {
isTimeToTap = YES;
}
if (ranBefore == NO) { //create labels
//tell when to tap
tapTellerTop.text = @"Tap when the answer is 6!";
tapTellerBottom.text = @"Tap when the answer is 6!";
//make bottom label
mathDisplayBottom = [[UILabel alloc] initWithFrame:CGRectMake(15, 250, 242, 92)];
mathDisplayBottom.font = [UIFont fontWithName:@"Helvetica" size: 96.0];
mathDisplayBottom.textColor = [UIColor whiteColor];
mathDisplayBottom.backgroundColor = [UIColor clearColor];
[self.view addSubview: mathDisplayBottom];
//make top label
mathDisplayTop = [[UILabel alloc] initWithFrame:CGRectMake(55, 120, 242, 92)];
mathDisplayTop.font = [UIFont fontWithName:@"Helvetica" size: 96.0];
mathDisplayTop.textColor = [UIColor whiteColor];
mathDisplayTop.backgroundColor = [UIColor clearColor];
[self.view addSubview: mathDisplayTop];
//rotate top label
mathDisplayTop.transform = CGAffineTransformMakeRotation(180.0 /180.0 * M_PI);
}
//if ran before just update the text
mathDisplayBottom.text = [mathProblems objectAtIndex:problemSelector];
mathDisplayTop.text = [mathProblems objectAtIndex:problemSelector];
ranBefore = YES; //if its here. its been ran.
//run timer wait for (3) then loop again until userTaps = YES
[self performSelector:@selector(simpleMath) withObject:nil afterDelay:3.0f];
[mathProblems release];
[mathAnswers release];
[mathDisplayBottom release];
[mathDisplayTop release];
}
答案 0 :(得分:0)
你永远不应该sleep()
。要重复调用函数,请调用:
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(doStuff:)
userInfo:nil repeats:YES];
然后定义函数:
-(void)doStuff:(NSTimer*)timer
{
// stuff
if ( iAmDone ) [timer invalidate];
}
或如果你想发起另一个电话,5秒后你可以打电话
if ( !iAmDone ) [self performSelector:@selector(simpleMath) afterDelay:5];
在simpleMath
的末尾。
答案 1 :(得分:0)
睡眠将阻止您的应用程序运行或响应。以下是您可以使用的视图控制器的快速部分示例。这假设您连接了水龙头按钮并仅使用其中一个标签等,但您可以使用它。此外,这不会处理内存问题或任何内容,因此您需要添加支持。
但是,一旦创建了此视图控制器并安装了视图,数学问题将每5秒更新一次。如果用户按下按钮并且答案有效,我们会记录成功消息,否则我们会记录失败消息。
@interface myMathController : UIViewController {
NSArray* mathProblems;
NSIndexSet* validIndexes;
NSUInteger currentIndex;
NSTimer* timer;
}
@property(nonatomic,assign) IBOutlet UILabel* mathDisplayLabel;
- (void)updateProblem:(NSTimer*)timer;
- (IBAction)userTap;
@end
@implementation myMathController
- (void)viewDidLoad
{
[super viewDidLoad];
mathProblems = [[NSArray alloc] initWithObjects:@"6 - 1",@"2 + 3",@"3 x 2",@"3 x 1",@"2 x 4",nil];
// Add all valid answers
NSMutableIndexSet* tempIndexes = [[NSMutableIndexSet alloc] init];
[tempIndexes addIndex:2];
validIndexs = [tempIndexes copy];
[tempIndexes release];
timer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:5.0 target:self selector:@selector(updateProblem:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
- (void)updateProblem:(NSTimer*)timer
{
currentIndex = arc4random() % [mathProblems count];
[mathDisplayLabel setText:[mathProblems objectAtIndex:currentIndex]];
}
- (void)userTap
{
if( [validIndexes containsIndex:currentIndex] ) {
NSLog(@"This user is SMART!");
} else {
NSLog(@"This user needs more work!");
}
}
- (void)dealloc
{
[timer invalidate];
[timer release];
[mathProblems release];
[validIndexes release];
[super dealloc];
}
@end