我想更新從一個循環的標籤,例如像這樣:iOS中的UI元素如何從沒有線程的循環中更新?
- (void)viewDidLoad {
[super viewDidLoad];
int i=0;
while (1) {
i++;
[NSThread sleepForTimeInterval:0.05]; // do some computation,
[myLabel setText:[NSString stringWithFormat:@"%d", i]]; // show the result!
[self.view setNeedsDisplay];
}
}
假設,而不是睡眠一些繁重的計算完成。 我不想在後臺執行計算的開銷。 當於Windows中處理這個問題將是.DoEvents,如下例所示: http://www.tek-tips.com/viewthread.cfm?qid=1305106&page=1
是否有iOS的這種類似的解決方案?
[self.view setNeedsDisplay]不工作了!
必須有一些方法,以處理來自iOS設備進行控制的日程安排應用程序事件在主線程中......如同在Windows .DoEvents儘管它所有的缺點是對於一些簡單的應用程序非常有用。
我想這就像一個遊戲循環,但與UI組件。
嘗試使用performSelector:withObject:afterDelay: http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/performSelector:withObject:afterDelay:inModes: – AmineG 2011-06-12 18:25:53
[self.view performSelector:@selector(setNeedsDisplay)withObject:nil afterDelay:0];不工作... – brbr 2011-06-12 18:45:03
[self performSelectorOnMainThread:@selector(heavyDutyComputation)withObject:nil waitUntilDone:YES];不起作用... – brbr 2011-06-13 06:46:07