无法在mapview上显示UIActivityIndi​​cator

时间:2014-07-29 20:34:26

标签: ios objective-c uiactivityindicatorview

在我的代码中,我初始化了一个UIActivityIndi​​cator,并将其添加为子视图,但它从未出现过。出了什么问题?

-(void) update {

    UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityView.center=self.view.center;
    [self.view addSubview:activityView];
    [activityView startAnimating];

    /** Stuff to  access to server, this stuff works correctly.*/
    [activityView stopAnimating];
}

1 个答案:

答案 0 :(得分:0)

由于您startingstoping活动与runloop相同,因此更新UI不会发生变化。您正在全力以赴server stuff在同一个synchronously上阻止runLoop并且没有给activity指标启动时间。您应该在下一个runLoop启动服务器内容,这样活动指标就会有时间开始使用performSelector,延迟时间为0.0,它会启动activity indicator而不是立即执行服务器内容runloop而不是停止activityInddicator

-(void) update {

    UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityView.center=self.view.center;
    [self.view addSubview:activityView];
    [activityView startAnimating];
    //Use performSelector with 0.0 so activity indicator starts  
    [self performSelector:@selector(thisWillStartAtNextRunLoop) withObject:nil afterDelay:0.0];
}

-(void)thisWillStartAtNextRunLoop{

    /** Stuff to  access to server, this stuff works correctly.*/
    [activityView stopAnimating];
}

修改main thread执行,因为所有的UI更新都必须在main thread上 试试

-(void) update {
  dispatch_async(dispatch_get_main_queue(), ^{

    UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityView.center=self.view.center;
    [self.view addSubview:activityView];
    [activityView startAnimating];
 });
    /** Stuff to  access to server, this stuff works correctly.*/
}