问题我在iphone sdk中加载进度指示器?

时间:2011-07-19 10:04:17

标签: iphone objective-c ipad

我想从服务器获取列表需要一些时间所以当时我正在显示进度指示器。我的问题有时会出现进度指示器,有时候它没有出现。我使用的代码是

-(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 ..

1 个答案:

答案 0 :(得分:0)

您正在主线程上进行所有活动。您的应用似乎会被阻止,因为您阻止了UI线程。

如果它只是在主线程上调用一个方法而不是等待它完成,那么启动NSThread的用途是什么?你根本就没有多线程。

你的应用就是这样:

主线程中的

  • 显示等待视图
  • 执行1次循环循环
  • 进行数据处理
  • 隐藏等待视图

由于您的数据处理在主线程中,因此您可以阻止其中的所有内容。您应该在辅助线程中进行处理并保留主线程以进行UI处理。

相关问题