如何在从Web服务获取数据时在视图加载期间添加活动指示符

时间:2013-01-31 06:51:48

标签: iphone ios objective-c ios6.1

我如何在viewload之前添加UIActivityIndicatorView。我从网络服务器获取数据。

这是我的代码

spinner=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(225, 115, 30, 30)];
[spinner setBackgroundColor:[UIColor clearColor]];
[spinner setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[self.view addSubview:spinner];
[spinner startAnimating];


objCountryPreferences=[[CountryPreferences alloc]init];
objLanguagePreference=[[LanguagePreference alloc]init];
objMobilePrefixParser=[[MobilePrefixParser alloc]init];

//这是调用不同的网络服务

[objMobilePrefixParser getMobilePrefix];
[objLanguagePreference  languagePreference];
[objCountryPreferences getCountryIdArr];

3 个答案:

答案 0 :(得分:1)

在单独的方法中添加ActivityIndi​​cator并使用

调用它
[self performSelector:@selector(addActivityIndicator) withObject:nil afterDelay:0.1];

答案 1 :(得分:0)

您的意思是进度指示器不是动画。如果是,则需要在单独的线程中执行startAnimating。 [spinner performSelector:@selector(startAnimating)]

答案 2 :(得分:0)

创建微调器:

  self.spinner=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.spinner.frame=CGRectMake(152,183,60,60);
    // display spinner in StatusBar it Optional for you
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    [self.spinner sizeToFit ];    
      /*-----------Change Size of UIActivityIndicatorView-------------*/
        self.spinner.transform = CGAffineTransformMakeScale(2, 2);
    [self.webView addSubview:self.spinner];

使用UIWebView的Delegate方法:

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [self.spinner startAnimating];
     // display  spinner in StatusBar it Optional for you
     [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.spinner stopAnimating];
    // Remove  spinner in StatusBar it Optional for you
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

}

我的代码可能对您有所帮助:)