2012-01-04 67 views
0

我有一個在IB中創建的視圖。它裏面我有一個編程創建的滾動視圖。在滾動視圖中,我連接到Web服務並獲取內容和圖像。我想在做這個時顯示一個活動指標。所以,我有:增加了滾動無法獲取要在iPhone應用上顯示的活動指示器

並經過
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
    activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0); 
    activityIndicator.center = self.view.center; 
    [self.view.window addSubview:activityIndicator]; 

權利,我有:

[self.view addSubview:scrollView]; 
// ADD Scroll View Ends 


    //Add the Lisitng Image 
    NSOperationQueue *queue = [NSOperationQueue new]; 
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] 
             initWithTarget:self 
             selector:@selector(loadImage) 
             object:nil]; 
    [queue addOperation:operation]; 

而且在我的LoadImage有:

- (void)loadImage { 

    [activityIndicator startAnimating]; 

我已經試過[self.view .window addSubview:activityIndi​​cator],; [self-> ScrollView addSubview:activityIndi​​cator],; [self.view addSubview:activityIndi​​cator];

但我只是不能讓指標顯示。任何人都知道什麼可能是錯的?

+0

在這裏發佈您的整個.m類。這是因爲您添加該活動指示符的順序。 – 2012-01-04 11:00:15

+0

是的,Ankit可能在這裏的正確軌道上。你的UIScrollView是否有透明背景?如果沒有,那麼你實際上是掩蓋了UIActivityIndi​​catorView。你可以隨時在[self.view addSubView:scrollView]之後;調用[self.view bringSubviewToFront:activityIndi​​cator]; – 2012-01-04 11:21:01

回答

4

你應該這樣做viewDidLoad中

- (void)viewDidLoad 
{ 
    //Start Activity Indicator View 
    indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    indicatorView.frame = CGRectMake(40.0, 20.0, 60.0, 60.0); 
    indicatorView.center = self.view.center; 
    [self.view addSubview:indicatorView]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    [indicatorView startAnimating]; 

    [self performSelectorInBackground:@selector(loadscroll) withObject:self]; 
} 

注:「performSelectorInBackground」會告訴你在查看活動指標和你loadScroll方法將獲取從互聯網上的所有數據和做其他工作。

-(void)loadscroll 
{ 
    //Your Scroll View 
     //Your other Data Manipulation even from internet 
     //When data is fetched display it 
     [self removeActivityIndicator]; //To stop activity Indicator  
} 

- (void)removeActivityIndicator 
{ 
     [indicatorView stopAnimating]; 
} 
+0

哈立德。我試過並實施了這個,但似乎沒有解決我的問題。我注意到的一件事是,當應用程序從後臺恢復時,我可以看到指示器。我已經按照您的建議將所有代碼放入loadscroll中。但活動指標仍然隱形 – 2012-01-04 12:23:31

+0

Savas Zorlu!它的工作,我先確認它,然後發佈答案。你可以發送我的.m文件。我的電子郵件ID是[email protected]。 – 2012-01-04 13:07:20

+0

謝謝哈立德。我已將它發送到您的電子郵件 – 2012-01-04 18:44:43

0

我多次面對活動指標上面的問題後發現的。

  • 活動指示燈和活動指示燈出現後需要執行的功能(或方法或代碼)無法在主線程上運行。如果是這樣,活動指示器將不會出現。解決方案是在主線程上運行活動指示器,並在後臺線程中運行該功能。
  • 我也遇到了一個情況,其中活動指示器後面的函數不能在後臺線程中運行。在我的情況下,它正在播放使用MPMoviePlayerController的音樂文件,無法在後臺線程中執行。我所做的解決此問題的方法是在主線程中運行活動指示器,然後使用NSTimer在延遲後調用我的方法,該時間足以使活動指示器出現並開始動畫。
  • 我還發現活動指示器必須在performSelectorOnMainThread中調用,並且應該在給定的viewController的頂視圖上添加一個子視圖,一旦它開始動畫。 一旦不需要,它應該通過調用removeFromSuperView方法來刪除。
相關問題