繁体   English   中英

如何强制UIButton刷新?

[英]How to Force UIButton to Refresh?

我有以下工作片段:

for (UIButton *b in buttonMapping) {
    [b setTitle:@"yo!" forState:UIControlStateNormal];
    [NSThread sleepForTimeInterval:1.0];
}

有四个按钮,所有四个按钮都将更新。 但是,而不是每秒更新一次,而是经过了四秒钟,并且它们都更新了。

如何强制UIButton更新? 还是这不是推荐的睡觉方法?

[b setNeedsDisplay];

我也不建议您休眠主线程(就像您在此处所做的那样),因为这会禁用所有用户交互。

有两种选择。 一种可能是使用NSTimer每秒执行一次特定方法。 但是,更简单的方法是执行以下操作:

for (NSUInteger idx = 0; idx < [buttonMapping count]; idx++) {
  UIButton * b = [buttonMapping objectAtIndex:idx];
  [b performSelector:@selector(setNormalStateTitle:) withObject:@"yo!" afterDelay:(idx*60)];
}

然后,将一个名为setNormalStateTitle:的方法添加到UIButton(即类别),该方法仅执行setTitle:forControlState:方法。 使用这种方法,您根本不需要setNeedsDisplay方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM