iphone - 如何强制UIButton刷新?

标签 iphone objective-c cocoa-touch

我的工作片段如下:

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

有四个按钮,所有四个按钮都会更新。然而,不是每秒更新一个,而是四秒过去并且它们全部更新。

如何强制 UIButton 更新?或者这不是推荐的 sleep 方法?

最佳答案

[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)];
}

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

关于iphone - 如何强制UIButton刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1622036/

相关文章:

ios - 使用 UIImagePickerController 使 UIView 显示相机 View

ios - AFHTTPSessionManager 问题

iphone - XCode 中 iPhone 4 启动屏幕的尺寸 + 名称和图标

objective-c - 如何使用 iCloud 存储和同步应用程序文件

iphone - 使用Http Post成功上传到服务器后,图像变为水平

iphone - 使用 Storyboard 和 UIActionSheet 为新 View 创建模型效果

ios - 为什么 WKInterfaceTable 的方法 rowControllerAtIndex 返回 nil?

iphone - 通过检测 URL scheme 替换本地资源

iphone - 移除或重新激活 uivew 层上的 CABasicAnimation

ios - 在不使用自动布局的情况下调整 iPhone 5 和 iPhone 3.5 的屏幕尺寸