如何强制UIButton刷新? [英] How to Force UIButton to Refresh?

查看:56
本文介绍了如何强制UIButton刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下工作片段:

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

有四个按钮,所有四个按钮都将更新.但是,不是每秒更新一次,而是四秒钟.他们都更新.

There's four buttons, all four buttons update. However, instead of updating one per second, four seconds go by & they all update.

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

How can I force UIButton to update? Or is this not the recommend method for sleeping?

推荐答案

[b setNeedsDisplay];

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

I'd also not recommend sleeping the main thread (like you're doing here), since that disables all user interaction.

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

There are a couple of alternatives. One might be to use an NSTimer to execute a particular method once a second. However, the easier method would be to do something like:

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 方法.

Then add a method to UIButton (ie, a category) called setNormalStateTitle: that simply executes the setTitle:forControlState: method. With this approach, you won't need the setNeedsDisplay method at all.

这篇关于如何强制UIButton刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆