我對這兩個的使用略有困惑。代表和performSelectorOnMainThread
我有一個後臺線程,負責下載數據並將其應用到iOS設備中的核心數據數據庫。
後臺線程中的代碼調用共享實例類ProgressController來更新UI(我知道它在主線程中運行)的進度。然後ProgressController擁有一個由View Controller分配的委託。
它的所有工作都很好,除了一旦後臺線程啓動後UI不更新。我知道委託人被調用,因爲我有NSLogs正在通過正在傳遞的文本射擊。
現在我讀了,我應該使用performSelectorOnMainThread,但這似乎是多餘的,因爲委託正在發射。
我應該使用performSelectorOnMainThread,而不是完全不使用委託。
我錯過了什麼嗎?
如果有人能解釋,我會很感激。
謝謝,
Chris。
在後臺線程
progressController = [ProgressController sharedInstance];
[progressController open];
....
[progressController updateProgress:NSLocalizedString(@"Update text here", @"Update text here")];
在ProgressController.h
#import <Foundation/Foundation.h>
@protocol ProgressControllerDelegate
@required
- (void) displayProgress:(NSString *)text;
- (void) showProgress;
- (void) hideProgress;
@end
@interface ProgressController : NSObject {
NSString *currentProgress;
BOOL progressOnDisplay;
id delegate;
}
+ (ProgressController *)sharedInstance;
@property (nonatomic) BOOL progressOnDisplay;
@property (nonatomic, assign) id delegate;
-(void) open;
-(void) updateProgress:(NSString *)text;
-(void) reDisplayProgress;
-(void) close;
@end
在ProgressControll er.m #進口「ProgressController.h」
@implementation ProgressController
@synthesize progressOnDisplay;
@synthesize delegate;
static ProgressController *sharedInstance;
+ (ProgressController *)sharedInstance {
@synchronized(self) {
if (!sharedInstance)
[[ProgressController alloc] init];
}
return sharedInstance;
}
+(id)alloc {
@synchronized(self) {
NSAssert(sharedInstance == nil, NSLocalizedString(@"Attempted to allocate a second instance of a singleton ProgressController.", @"Attempted to allocate a second instance of a singleton ProgressController."));
sharedInstance = [super alloc];
}
return sharedInstance;
}
-(id) init {
if (self = [super init]) {
[self open];
}
return self;
}
// Ask delegate to show new Progress Label
-(void) open {
progressOnDisplay = TRUE;
currentProgress = @"";
[self.delegate showProgress];
}
// Ask delegate to update and display Progress text
-(void) updateProgress:(NSString *)text {
currentProgress = text;
[self.delegate displayProgress:currentProgress];
}
// Ask delegate display existing Progress text if any
-(void) reDisplayProgress {
if (currentProgress != @"") {
[self.delegate displayProgress:currentProgress];
[self.delegate showProgress];
}
}
// Ask delegate to clear and hide Progress Label
-(void) close {
progressOnDisplay = FALSE;
currentProgress = @"";
[self.delegate hideProgress];
}
@end
在視圖控制器
- (void)viewDidLoad {
[super viewDidLoad];
progressController = [ProgressController sharedInstance];
progressController.delegate = self;
[progressController reDisplayProgress]; // In case progress has been updated prior to the view load
}
// Delegate method to show Progress Label
- (void) showProgress {
progressView.hidden = FALSE;
}
// Delegate method to display specific text in Progress label
- (void) displayProgress:(NSString *)text {
[progressLabel setText:text];
[progressView setNeedsDisplay];
DLog(@"Reporting - %s", [text UTF8String]); // I can see that this is firing successfully
}
// Delegate method to hide Progress Label
- (void) hideProgress {
progressView.hidden = TRUE;
}
請在後臺線程中顯示調用ProgressController方法的代碼。 – Eiko 2011-04-18 11:33:39