【发布时间】:2013-04-01 09:07:13
【问题描述】:
我读到了一些UI界面只在MainThread上更新的信息。
我需要异步更新一些UIButtons,所以我使用performSelectorInBackground,它在模拟器和设备(iPad4)上工作正常。
[self performSelectorInBackground:@selector(toggleToUpperWhenSingleShift) withObject:nil];
- (void)toggleToUpperWhenSingleShift{
shiftStateIndicator = 1;
for (UIView *aPad in self.subviews) {
if ( [aPad isKindOfClass:[UIButton class]] ) {
UIButton *aButPad = (UIButton *)aPad;
NSMutableString *currentTitle = [NSMutableString stringWithString:[aButPad titleForState:UIControlStateNormal]];
NSString *firstChar = [currentTitle substringToIndex:1];
[currentTitle replaceCharactersInRange:NSMakeRange(0, 1) withString:[firstChar uppercaseString]];
[aButPad setTitle:currentTitle forState:UIControlStateNormal];
[aButPad setTitle:currentTitle forState:UIControlStateHighlighted];
currentTitle = [NSMutableString stringWithString:[aButPad titleForState:UIControlStateSelected]];
firstChar = [currentTitle substringToIndex:1];
[currentTitle replaceCharactersInRange:NSMakeRange(0, 1) withString:[firstChar uppercaseString]];
[aButPad setTitle:currentTitle forState:UIControlStateSelected];
}
}
}
我担心如果我保留我的代码会发生一些不需要的功能。谁能详细解释一下performSelectorInBackground?
为什么不使用它来更新 UI 以及为什么我的应用可以使用它? 无论如何调试问题将不胜感激!
【问题讨论】:
-
我需要异步更新一些 UIButtons 是什么意思?在 UIView 文档链接下方(阅读 Threading Considerations)developer.apple.com/library/ios/#documentation/UIKit/Reference/…
-
performSelectorInBackground 与普通方法是异步的。我理解错了吗?
-
阅读以下讨论stackoverflow.com/questions/11122957/…。
performSelectorInBackground这是一种同步方法。这意味着其中的代码将以串行方式执行。但在不同的线程中。例如,异步模式是 asyncNSURLConnection使用的模式。 -
谢谢!我误解了不同的线程是异步的。
标签: ios objective-c multithreading user-interface uiview