【问题标题】:Alternative delay function替代延迟功能
【发布时间】:2014-08-23 20:45:48
【问题描述】:

我使用以下代码来延迟更改文本:

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 并立即再次打开它,该功能将更改文本。我怎样才能修改它以允许取消?

【问题讨论】:

  • 看看NSTimer

标签: ios objective-c


【解决方案1】:
self.timer = [NSTimer scheduledTimerWithTimeInterval:delayInSeconds
                                              target:self
                                            selector:@selector(updateLabel)
                                            userInfo:nil
                                             repeats:NO];


- (void)updateLabel
{
   self.label.text = %"ye";
}

在你的viewWillDisapear:

[self.timer invalidate];

【讨论】:

    【解决方案2】:
    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;
    

    【讨论】:

      猜你喜欢
      • 2011-05-02
      • 1970-01-01
      • 1970-01-01
      • 2019-05-14
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多