UIActivityIndi​​cator未显示

时间:2013-01-17 12:03:38

标签: iphone xcode4.5 uiactivityindicatorview

我有一个装有tableView的FirstView,当点击tableviewCell的一个附件按钮然后它进入SecondView。SecondView中的数据是从服务URL加载的,所以需要时间。所以我需要在敲击附件按钮时显示activityIndi​​cator ,在加载整个数据后停止它。但是我看不到活动指示器显示。

这是我正在处理的代码:

- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{

 activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(143, 220, 37, 37)];
 activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
 activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
 activityIndicator.color = [UIColor redColor];
 [scrollView  addSubView:activityIndicator];
 [activityIndicator startAnimating];
 activityIndicator.frame=CGRectMake(140, 195, 37, 37);
 [activityIndicator startAnimating];

        SecondView *secView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
        secView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

        UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
        UILabel *name1 = (UILabel*)[cell viewWithTag:111];
        secView.provName = [name1 text];

        [self presentModalViewController: secView animated:NO];

        [secView release];
    }

我哪里出错?

编辑:

SecondView:

-(void)viewWillAppear:(BOOL)animated

{
    activityIndictr = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(143, 220, 37, 37)];
    activityIndictr.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;

    activityIndictr.color = [UIColor redColor];
    [testscroll addSubview:activityIndictr];
    [activityIndictr startAnimating];

}

-(void)viewDidLoad
{
  [super viewDidLoad]
   // I m calling many service urls(10) and assigning to labels and tableview 
  [activityIndictr stopAnimating];

}

4 个答案:

答案 0 :(得分:1)

您应该在SecondView的ViewWillAppear中调用活动指示器,以便在推入此视图后立即启动活动指示器,然后在从网址解析所有数据时将其关闭。

这是因为第二视图控制器几乎会立即被推送,数据需要时间来加载

答案 1 :(得分:0)

您分配两个活动指示符,抛弃第一个活动指示符,然后开始动画第二个两次,并且永远不会将任何活动指示符添加到任何现有视图中。难怪它没有出现。试试[[secView view] addSubview:activityIndicator]或类似的东西。

答案 2 :(得分:0)

在SecondView的viewWillAppear中执行此操作:

activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(143, 220, 37, 37)];
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[self.view addSubview:activityIndicator];
[activityIndicator startAnimating];

答案 3 :(得分:0)

我再次更新了答案。

将此代码放在appdelegate中并在appdelegate中定义静态对象,如下所示

static UIActivityIndi​​catorView * activityView;

+(void)showActivityIndicator{

if (!activityView) {
    activityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityView.center = CGPointMake(160, 230);
    [activityView startAnimating];
    UIWindow *win = [UIApplication sharedApplication].keyWindow;
    [win addSubview:activityView];
}
}


+(void)removeActivityIndicator{
if ([activityView superview]) {
    [activityView removeFromSuperview];
    activityView = nil;
}
}






- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
UIWindow *window = [UIApplication sharedApplication].keyWindow;

//global static activity indicator... you can access from any other view controlller

 [AppDelegate showActivityIndicator];

}

获得回复后

   [AppDelegate removeActivityIndicator];