iOS活动指示器问题

时间:2016-08-30 09:17:41

标签: ios objective-c

我在子视图中创建UITableview。对于填充表我使用一些jsonget请求。在此过程中,我想显示一个活动指标。我不知道为什么它没有显示。

示例代码:

activityIndicator1 = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityIndicator1.center = CGPointMake([[UIScreen mainScreen]bounds].size.width/2, [[UIScreen mainScreen]bounds].size.height/3);
[self.window addSubview:activityIndicator1];
activityIndicator1.color=[UIColor greenColor];
[self bringSubviewToFront:activityIndicator1];
[UIApplication sharedApplication].networkActivityIndicatorVisible = TRUE;

[activityIndicator1 startAnimating];

问题是活动指示器没有显示。

谢谢和问候

维卡斯

5 个答案:

答案 0 :(得分:0)

您需要在子视图中添加 activityIndi​​cator1

即。

[self.subView addSubView: activityIndicator1];

activityIndi​​cator1的中心应该是您的子视图中心。

activityIndicator1.center = subView.center;

希望这有帮助。

由于

答案 1 :(得分:0)

将此内容添加到.h文件中,

@property(nonatomic, retain) UIActivityIndicatorView *indicator;

而不是在按钮点击方法中添加此代码,

indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        indicator.frame = CGRectMake(0.0, 0.0, 60.0, 60.0);
        indicator.color = [UIColor colorWithRed:0.949 green:0.571 blue:0.558 alpha:1.000];
        [indicator setBackgroundColor:[UIColor colorWithRed:1 green:1.000 blue:1 alpha:1.000]];
        indicator.center = self.view.center;
        [self.view addSubview:indicator];
        [indicator bringSubviewToFront:self.view];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = TRUE;

        [indicator startAnimating];

比你想要使用的停止指示器

[indicator stopAnimating];

希望这会对你有所帮助。

答案 2 :(得分:0)

默认情况下,UIActivityIndi​​cator' hidesWhenStopped属性为YES。此外,默认情况下它已停止。所以你的活动指标就是隐藏的。添加以下代码:

activityIndicator1.hidden = NO;

答案 3 :(得分:0)

当您使用来自api请求的新数据刷新表格时,一个常见且方便的工具是UIRefreshControl,当用户拉出表格进行刷新时,它将显示为ActivityIndi​​cator旋转,同时打电话来提取新数据。

@property (strong, nonatomic)  UIRefreshControl   *refreshControl;

然后在viewDidLoad内创建refreshControl并添加目标操作

-(void)viewDidLoad{

   [super viewDidLoad]

    self.refreshControl = [[UIRefreshControl alloc]init];
   [self.refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged];
   [yourTableView addSubview:self.refreshControl];
}

通过向下拉动桌子,活动指示器将显示,并且将进行刷新表格的调用。

-(void)refreshTable{
   // Here your call to pull data from your API to refresh the data
   // ** Your code here to pull in your JSON data and update your datasource **
   //Once the data has been retrieved or failed to retrieve, call this to end the refreshing

   [self.refreshControl endRefreshing];
 }

如果这是您尝试重新创建的内容,我希望这会有所帮助。

注意 - 用户需要在向下的 pull-to-refresh '中拉出表格。当然要激活这个行动

答案 4 :(得分:0)

改为执行此操作

UIView*  WaitingView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
WaitingView.backgroundColor=[UIColor blackColor];
WaitingView.alpha=0.5;
 UIActivityIndicatorView*   activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:
                    UIActivityIndicatorViewStyleWhiteLarge ];
    [activityView setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)];
    [activityView setContentMode:UIViewContentModeCenter];

[WaitingView addSubview:activityView];
[self.view addSubview:WaitingView];
[activityView startAnimating];
相关问题