0

我想在单击 UIButton 时添加一个活动指示器并继续为其设置动画,直到加载来自服务 url 的数据。如果加载,则它移动到下一个视图。但我无法这样做..

这是我的代码..

 -(IBAction)switchtoGetProviders
    {
        UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(225, 115, 30, 30)];
        [activity setBackgroundColor:[UIColor clearColor]];
        [activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
        [self.view addSubview:activity];
        [activity release];
        [activity startAnimating];

        NSURL *urlString= [NSString stringWithFormat:@"http://www.something.net/service.svc/Xml"];

        if(arr.count>0)
        {
            [activity stopAnimating];
            ViewController2 *gp=[ViewController2 alloc];
            [self presentModalViewController:gp animated:NO];
            [gp release];
        }
else
{
 UIAlertView *alert = [[[UIAlertView alloc]initWithTitle: @"Results not found"
                                                                  message: @""
                                                                 delegate: self
                                                        cancelButtonTitle:@"OK"
                                                        otherButtonTitles:nil]autorelease];


                  [alert show];         
       }
    }

实际上,当 arr 为空时,我会得到一个alertView. 出现时alertView然后activityindicator开始。当我点击确定按钮时,alertView它会停止动画。但我希望它在被触摸时立即开始。UIbutton我该怎么做?我哪里错了?

4

2 回答 2

1

在停止动画之前不要释放它。如果你释放它,那么你就会失去它的参考。

 [activity release];//Dont Release here 
 [activity startAnimating]; 



     [activity stopAnimating];
[activity release];
于 2012-11-07T10:48:08.710 回答
0

您的代码存在多个问题。

首先是早期的发布,其次你不给 UI 任何时间来添加它UIActivityIndicatorView并让它开始动画。你也忘记了initViewController2分配的。

我也猜想你还没有发布你所有的代码,因为NSURL *urlString= [NSString stringWithFormat:@"http://....不会创建一个有效的NSURL对象。我也很想知道你arr从哪里得到变量。

如果您真的想用一种方法完成所有操作,则必须使用 GCD。

-(IBAction)switchtoGetProviders
{
    UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(225, 115, 30, 30)];
    [activity setBackgroundColor:[UIColor clearColor]];
    [activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [self.view addSubview:activity];
    [activity startAnimating];

    int64_t delayInSeconds = 0.1;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        NSURL *urlString= [NSString stringWithFormat:@"http://www.something.net/service.svc/Xml"];

        if(arr.count>0) {
            [activity stopAnimating];
            [activity  removeFromSuperview];
            [activity release];
            ViewController2 *gp= [[ViewController2 alloc] init];
            [self presentModalViewController:gp animated:NO];
            [gp release];
        } else { 
            UIAlertView *alert = [[[UIAlertView alloc]initWithTitle: @"Results not found" message: @"" delegate: self cancelButtonTitle:@"OK" otherButtonTitles:nil]autorelease];
            [alert show];
            [alert release];
    });
}
于 2012-11-07T10:52:24.543 回答