UIActivityIndi​​catorView无法正常工作

时间:2015-10-14 17:56:33

标签: ios objective-c

我在viewDidLoad中进行了以下初始化:

[self.view addSubview:self.indicator];
//self.indicator.hidden = NO;
[self.indicator startAnimating];

现在,如果我添加以下2行,则指示符显示并旋转:

- (void)pressButton {
    [self.view addSubview:self.indicator];
    [self.indicator startAnimating];
    dispatch_async(dispatch_get_main_queue(), ^{

        WebAccess *web = [WebAccess new];
        NSDictionary *dicResults = [web logon:self.email.text :self.password.text];
        if([[dicResults valueForKey:@"StatusCode"] intValue] == 200){// ALL IS OK
            appDelegate.accessToken = [dicResults valueForKey:@"AccessToken"];
            dicResults = [web getAccount:appDelegate.accessToken];
            NSLog(@"dicResults after getaccount: %@", dicResults);
            if([[dicResults valueForKey:@"StatusCode"] intValue] == 200){//dicResults holds a new object now!
                NSArray *usersArray = [dicResults valueForKeyPath:@"Account.Users"];
                NSLog(@"usersArray: %@", usersArray);//CORRECT DATA RECEIVED: YES
                if(usersArray.count){
                    [[appDelegate cdh]deleteAllFromEntityWithName:@"AccountData"];
                    [[appDelegate cdh]deleteAllFromEntityWithName:@"UserData"];//------------- REMOVE ALL WHEN LOADING NEW USER. AMYBE TOO QF. CHECK WITH DESIGNER
                }else{
                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Error" message:@"No users found on this account!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
                    [alertView show];
                }
               //more code........
                appDelegate.users = [NSMutableArray arrayWithArray:[[appDelegate cdh] fetchUsers]];
                if(appDelegate.users.count){
                    NSArray *currentUserArray = [NSArray arrayWithArray:[[appDelegate cdh]fetchCurrentUser]];
                    appDelegate.currentUser = [currentUserArray lastObject];
                    if(appDelegate.currentUser==nil)
                        appDelegate.currentUser = appDelegate.users[0];
                }
                [menu goToVC:1];

            }
        }else if([[dicResults valueForKey:@"StatusCode"] intValue] == 201){
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Wrong email or password!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alertView show];
        }
        [self.indicator stopAnimating];
    }); 
}

这不是我想要的,因为当我调用我的方法时,指针应首先显示,我们称之为pressButton。然后我会做以下事情:

No Wrap - In <head>

我可以发誓我昨天在测试时工作,但现在它没有。奇怪的是,如果我将这两条相同的两条线从我的按钮方法放入viewdidload,指示器会显示,但是当我在我的方法中执行它(但是在调度之外的obv)它从未显示....

任何帮助表示赞赏

1 个答案:

答案 0 :(得分:1)

将长时间运行的进程转移到自己的线程上。保持主线程打开以进行UI更新。

- (void)pressButton
{
    [self.view addSubview:self.indicator];
    [self.indicator startAnimating];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // Code for your long running process to run in the background...

        // Stop your activity indicator back on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.indicator stopAnimating];
        });
    });
}