2011-01-21 50 views
7

目標是「在啓動viewWillAppear時啓動spinner圖形,在顯示tableview之前加載數據」,所以用戶不會疑惑爲什麼在查看錶格之前會有延遲。即一個UIActivityIndi​​catorView已被添加到窗口中,我只是想設置alpha來隱藏/顯示它。崩潰 - 「Collection <CALayerArray:0x645dfc0>在枚舉時發生了變化。」

我啓動一個線程時,確保「旋轉齒輪」 ImageView的(標記= 333)被所示,移動到中加載/ viewWillAppear中計算的東西之前得到這個奇怪的錯誤。

我不明白這一點在每次調用[appdel addGearz]和[appdel removeGearz],它發生了這兩個,這是隨機的。它可能發生在2個viewWillAppears之後,或15個之後。如果我註釋掉設置alpha的那一行,那麼所有東西都可以工作。

一個典型的viewWillAppear中看起來像這樣,

[super viewWillappear]; 
[email protected]"Products listing"; //and other simple things 
[appdel addGearz]; 
[self getProducts]; 
[self getThumbnails]; 
[myTableView reloadData]; //in case view already loaded and coming back from subview and data changed 

這裏是崩潰,如果與阿爾法行沒有被註釋掉

-(void)addGearz { 
    [NSThread detachNewThreadSelector:@selector(gearzOn) toTarget:self withObject:nil]; 
} 

-(void)removeGearz { 
    [NSThread detachNewThreadSelector:@selector(gearzOff) toTarget:self withObject:nil]; 
} 

- (void)gearzOn { 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    [window viewWithTag:333].alpha=1.0; 
     // 
     // [[window viewWithTag:333] setNeedsDisplay]; 
    [pool drain]; 
} 

- (void) gearzOff { 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    [window viewWithTag:333].alpha=0.0; 
     // 
     // [[window viewWithTag:333] setNeedsDisplay]; 
    [pool drain]; 
} 

我用別人的代碼的代碼,所以......任何明顯的你可以看到?當然,我必須能夠改變線程中的UIViews的alpha?我是否需要在某些「停止枚舉而我更改此代碼」中「嵌入」alpha-change?

我做到了不通過移動字母更改線上面的池頁頭或低於[池排水]崩潰,但後來我得到了很多的「有沒有到位池自動釋放 - 只是跑冒滴漏」 -messages 。

顯然,我不明白這個線程代碼。

回答

8

您不能嘗試在單獨的線程上修改UI。應該只在主線程上操作UI。

相反拆卸一個新的線程,你應該使用performSelectorOnMainThread:withObject:waitUntilDone:。這將確保該方法將在正確的線程上被調用。

+0

[自performSelectorOnMainThread:@selector(gearzOn)withObject:無waitUntilDone:YES];不會立即更新可見性,這是​​整個想法 - 因爲viewWillAppear將在推送視圖之前完成所有加載操作,所以用戶應該知道發生了什麼。 – 2011-01-21 12:20:11

相關問題