2011-07-19 54 views
0

我想從服務器獲取列表需要一些時間所以當時我正在顯示進度指示器。我的問題有時候會出現進度指示器,有時候不會出現。我使用的代碼問題我在iphone sdk中加載進度指示器?

-(void)viewWillAppear:(BOOL)animated 

{ 
    [self loadprogressIndicator]; 
} 

-(void)loadprogressIndicator 
{ 
    MessengerAppDelegate* appDelegate = (MessengerAppDelegate*) [ [UIApplication sharedApplication] delegate]; 
    [appDelegate showWaitingView]; 

    [NSThread detachNewThreadSelector:@selector(statusIndicatorForRefresh) toTarget:self withObject:nil]; 
} 

-(void)statusIndicatorForRefresh 
{ 
    NSAutoreleasePool* pool=[[NSAutoreleasePool alloc]init]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    [self performSelectorOnMainThread:@selector(loadList) withObject:nil waitUntilDone:NO]; 
    [NSThread exit]; 
    [pool release]; 
} 

-(void)loadList 
{ 
    MessengerAppDelegate* appDelegate = (MessengerAppDelegate*) [ [UIApplication sharedApplication] delegate]; 
    customerList = [appDelegate getCustomersArray]; 
    [theTableView reloadData]; 
    [appDelegate removeWaitingView]; 
} 

And in appdelegate.m I implemeted these two methods 

- (void)showWaitingView 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    CGRect frame = CGRectMake(90, 190, 32, 32); 
    UIActivityIndicatorView* progressInd = [[UIActivityIndicatorView alloc] initWithFrame:frame]; 
    [progressInd startAnimating]; 
    progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge; 
    frame = CGRectMake(130, 193, 140, 30); 
    UILabel *waitingLable = [[UILabel alloc] initWithFrame:frame]; 
    waitingLable.text = @"Processing..."; 
    waitingLable.textColor = [UIColor whiteColor]; 
    waitingLable.font = [UIFont systemFontOfSize:20]; 
    waitingLable.backgroundColor = [UIColor clearColor]; 
    frame = [[UIScreen mainScreen] applicationFrame]; 
    UIView *theView = [[UIView alloc] initWithFrame:frame]; 
    theView.backgroundColor = [UIColor blackColor]; 
    theView.alpha = 0.7; 
    theView.tag = 999; 
    [theView addSubview:progressInd]; 
    [theView addSubview:waitingLable]; 
    [progressInd release]; 
    [waitingLable release]; 
    [window addSubview:[theView autorelease]]; 
    [window bringSubviewToFront:theView]; 
    [pool drain]; 
} 

- (void)removeWaitingView 
{ 
    UIView *v = [window viewWithTag:999]; 
    if(v) [v removeFromSuperview]; 

}

誰能請幫助我。快樂編碼..

在此先感謝。

Praveena ..

回答

0

您正在主線程上完成所有活動。你的應用程序將因爲阻止UI線程而顯示爲被阻止。

還有什麼是啓動一個NSThread,如果它只是調用你的主線程的方法,甚至沒有等待它完成?你根本沒有做多線程。

你的應用程序做這樣的:

  • 顯示等待視圖
  • 做1運行循環週期
  • 做你的數據處理
  • 隱藏等待視圖

在主線程

自您的數據處理是在主線你阻止一切。您應該在輔助線程中執行處理並離開用於UI處理的主線程。

+0

你可以給我想法如何做到這一點 – praveena

+0

瞭解你如何編程iPhone和使用多線程是有點超出了一個stackoverflow問題的範圍。您還可以直接將UIViews添加到您的窗口。如果您的應用支持設備輪換,請注意各種問題。 –