我讀了一些UI界面只在MainThread上更新的信息。如果通過performSelectorInBackground控制UI會發生什麼?
我需要異步更新一些UIButton
s,所以我使用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
的細節嗎?
爲什麼不使用它來更新用戶界面,爲什麼它可以用我的應用程序? 無論如何要調試問題將不勝感激!
你是什麼意思*我需要異步更新一些UIButtons *?在UIView文檔的鏈接下面(閱讀**線程注意事項**)http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html –
performSelectorInBackground與普通方法是異步的。我明白錯了嗎? –
閱讀以下討論http:// stackoverflow。COM /問題/ 11122957/IS-IT-OK-來創建-A-的UIView-ON-A-後臺線程。 'performSelectorInBackground'是一種同步方法。這意味着其中的代碼將以串行方式執行。但在另一個線程。例如,異步模式是異步'NSURLConnection'使用的模式。 –