开发者

How can I make a BOOL have a value for a short time, then change in Objective-C

I have a Bool that I am using in an if statement. The variable starts out as NO, then I enter the if statement, which executes if the开发者_StackOverflow中文版 bool's value is NO. Then I change the value to YES at the end of the if statement so it won't execute again, standard stuff.

What I want to do is somehow change that variable YES at the end, but have it change back to NO after about five seconds. How can that be done?


You can use NSTimer:

[NSTimer scheduledTimerWithTimeInterval:5.0 
                                 target:self 
                               selector:@selector(resetSomeBoolean:) 
                               userInfo:nil 
                                repeats:NO];


- (void) resetSomeBoolean:(NSTimer *) timer {
   self.someBoolean = NO;
}

Also, make sure that this property (someBoolean) is atomic.


Perhaps set something like:

... your code ...
myBool = YES; // set some senteniel.

[NSTimer scheduledTimerWithTimeInterval:5.0 
                                 target:self 
                               selector:@selector(resetMyBool:) 
                               userInfo:nil 
                                repeats:NO];
return;
}

and add something like

- (void) resetMyBool:(NSTimer*)meTimer {
      myBool = NO;
}

to your class. But are you sure you need to do this - sounds easy to get a race condition.

Thanks,

Dw.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜