我使用以下代码来延迟更改文本:
int64_t delayInSeconds = 0.6;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
label.text = %"ye";}
我无法取消此功能,因此如果我通过单击“返回”关闭我的viewController并立即再次打开它,该功能将更改文本。我该如何重写以允许取消?
答案 0 :(得分:5)
self.timer = [NSTimer scheduledTimerWithTimeInterval:delayInSeconds
target:self
selector:@selector(updateLabel)
userInfo:nil
repeats:NO];
- (void)updateLabel
{
self.label.text = %"ye";
}
并在viewWillDisapear:
[self.timer invalidate];
答案 1 :(得分:0)
self.cancelled = NO; // cancelled is a BOOL property
int64_t delayInSeconds = 0.6;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if (!self.cancelled) {
label.text = %"ye";
}
}
...
// When back is tapped
self.cancelled = YES;