2013-01-17 38 views
0

我想在iOS中使用的活動指標,並不能夠。我遵循Stackoverflow上的線程並使用它。這是我寫的:UIActivity指標沒有顯示在iOS應用程序啓動

-(void)viewDidLoad 
{ 
    [NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:self withObject:self]; 

    UITapGestureRecognizer *tGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doThisForTap:)]; 
    tGR.numberOfTapsRequired = 1; 

    [myRollTypeLabel addGestureRecognizer:tGR]; 
    myRollTypeLabel.userInteractionEnabled = YES; 

    [self.scrollView addSubview:myRollTypeLabel]; 
} 

- (void) threadStartAnimating:(id)data 
{ 
self.activityIndicatorNew =[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
self.activityIndicatorNew.color = [UIColor redColor]; 
[self.activityIndicatorNew startAnimating]; 
} 
- (void)doThisForTap :(UIGestureRecognizer *)gest 
{ 
    //lots of computation. 
} 
- (void)viewWillDisappear:(BOOL)animated 
{ 
[self.activityIndicatorNew stopAnimating]; 
self.activityIndicatorNew.hidden = YES; 
} 

但是活動指標根本沒有顯示出來?在「doThisForTap」方法中,我進行計算並移至另一個UIViewController。但我看不到活動指標。我究竟做錯了什麼?如果您需要更多信息,請詢問。謝謝..

+1

我認爲NSThread無法在UIKit中使用(無線程安全)。 – Larme

+0

@Larme。這是我在我的問題中提到的線程http://stackoverflow.com/questions/1850186/iphone-uiactivityindicatorview-not-starting-or-stopping – RookieAppler

+0

嗯,我會做主線程中的動畫,並加載或任何如果它不觸及用戶界面,則希望在另一個線程中同時執行此操作。你可以「重新同步」,有時在最後一步和一步之間做一個小動畫。 – Larme

回答

1

它不會出現像你實際上與addSubview添加指示器視圖層次:

你實例化它,將其分配給一個屬性,並啓動它的動畫,但從來沒有實際將它添加到視圖層次結構(據我所知)。

要添加活動的指標到畫面中,你應該設置它的起源,然後將它添加到任何視圖它應該出現在:

self.activityIndicatorNew =[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
self.activityIndicatorNew.color = [UIColor redColor]; 

CGRect indicatorFrame = self.activityIndicatorNew.frame; 
indicatorFrame.origin.x = // x coordinate goes here; 
indicatorFrame.origin.y = // y coordinate goes here; 
self.activityIndicatorNew.frame = indicatorFrame; 

[self.view addSubview:self.activityIndicatorNew]; 

[self.activityIndicatorNew startAnimating]; 

你不應該做最上面的一個背景線程,因爲除了主線程之外,您不應該操縱視圖層次結構。

如果您確實需要在後臺線程中啓動指示器動畫(並非完全相信您從所展示的代碼中完成),那麼在該後臺線程中唯一安全的做法是調用startAnimating。在分離新線程之前,其他所有內容都應該放入viewDidLoad中。

但是,我會盡量在viewDidLoad中做所有事情,並且只在必要時使用後臺線程。

就我個人而言,我會使用Interface Builder進行此操作;我想不出有很多理由在代碼中實例化一個簡單的活動指示器。

+0

謝謝..我剛剛放棄了我所做的一切。剛剛在我的故事板上放了一個UIActivityIndi​​cator並從那裏開始。它工作..我昨天做了這個..它不會工作..再次感謝。 – RookieAppler

相關問題