早教吧 育儿知识 作业答案 考试题库 百科 知识分享

iOS如何在一个函数执行过程中,在某一步停下来,等待一个消息,或通知之类的东西,然后继续执行就如同做一个nstimer,把那个固定时间改成一个条件.求教.

题目详情
iOS 如何在一个函数执行过程中,在某一步停下来,等待一个消息,或通知之类的东西,然后继续执行
就如同做一个nstimer,把那个固定时间改成一个条件.求教.
▼优质解答
答案和解析
用线程吧 貌似没又windows下边的messagebox那样的函数
-(void)timerArrived:(id)sender{
timerFinished = YES;
}
-(void) waitTimeForSeconds:(NSTimeInterval) times{
timerFinished = NO;
NSTimer *delayForProgram = [NSTimer scheduledTimerWithTimeInterval:times target:self selector:@selector(timerArrived:) userInfo:nil repeats:NO];
while (!timerFinished) {
[[NSRunLoop currentRunLoop] addTimer:delayForProgram forMode:@"NSDefaultRunLoopMode"];
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[[NSDate alloc] initWithTimeIntervalSinceNow:times]];
}
[delayForProgram release];
}
这里时间按秒计算 当然也可以无止尽等待