我在 viewDidLoad 中有以下初始化:
self.indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.indicator setCenter:CGPointMake([[UIScreen mainScreen]bounds].size.width/2, [[UIScreen mainScreen]bounds].size.height/2)];
//self.indicator.hidden = YES;
[self.indicator setHidesWhenStopped:1];
self.indicator.color = [UIColor orangeColor];
self.indicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
现在,如果我添加以下两行,指示器会显示并旋转:
[self.view addSubview:self.indicator];
//self.indicator.hidden = NO;
[self.indicator startAnimating];
这不是我想要的,因为指示器应该在我调用我的方法时首先显示,我们称它为 pressButton。然后我会执行以下操作:
- (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];
});
}
我本可以发誓昨天测试时它可以正常工作,但现在没有了。奇怪的是,如果我将相同的两行从我的按钮方法放入 viewdidload,指示器显示,但是当我在我的方法中执行它时(但 obv 在调度之外)它永远不会显示....
感谢任何帮助
最佳答案
将长时间运行的进程转移到它自己的线程上。让主线程保持打开状态以进行 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];
});
});
}
关于ios - UIActivityIndicatorView 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33132364/