Bil*_*ill 2 iphone cocoa-touch objective-c
我有以下工作片段:
for (UIButton *b in buttonMapping) {
[b setTitle:@"yo!" forState:UIControlStateNormal];
[NSThread sleepForTimeInterval:1.0];
}
Run Code Online (Sandbox Code Playgroud)
有四个按钮,所有四个按钮都会更新.但是,不是每秒更新一次,而是四秒钟,它们都会更新.
如何强制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)];
}
Run Code Online (Sandbox Code Playgroud)
然后向UIButton(即类别)添加一个方法,setNormalStateTitle:该setTitle:forControlState:方法只是执行该方法.使用这种方法,您根本不需要该setNeedsDisplay方法.