Ios 带UIAlertView的UIActivity指示器

Ios 带UIAlertView的UIActivity指示器,ios,uiview,uiactivityindicatorview,Ios,Uiview,Uiactivityindicatorview,我正在开发一个应用程序,用户可以通过UIAlertView确认某些操作,如果他确认,我将调用一个处理该操作的方法,然后我准备弹出我所在的视图,在调用该方法后返回到另一个视图 我想显示UIActivityIndicatorView,如果用户按确认键的时间与执行方法并转到另一个视图所需的时间相同。我在正确的位置使用了startAnimating和stop animating,但我从未看到显示的UIUIActivityIndicator视图,一秒钟也没有 我想这与一些UI问题有关,因为UIAlertV

我正在开发一个应用程序,用户可以通过
UIAlertView
确认某些操作,如果他确认,我将调用一个处理该操作的方法,然后我准备弹出我所在的视图,在调用该方法后返回到另一个视图

我想显示
UIActivityIndicatorView
,如果用户按确认键的时间与执行方法并转到另一个视图所需的时间相同。我在正确的位置使用了
startAnimating
stop animating
,但我从未看到显示的UI
UIActivityIndicator视图,一秒钟也没有

我想这与一些UI问题有关,因为
UIAlertView
,但不确定我是否正确。我只需要了解如何在方法执行时正确使用
UIActivityIndicatorView

我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.activityIndicator.alpha = 1.0;
    self.activityIndicator.hidesWhenStopped = YES;
    self.activityIndicator.center = self.view.center;
    [self.view addSubview:self.activityIndicator];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 1) {

        [self.activityIndicator startAnimating];

        ContactsTableViewController *contactTableView = [self getContactsTVC];

        [contactTableView applyActionOnCells];

        // doing some setup before poping off to the root view controller of my nav controller 

        [self.activityIndicator stopAnimating];

        // then go to rootViewController
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

我不是100%确定,但是试着注释掉
停止动画制作
调用,看看它是否出现

如果这有帮助的话,
applyActionOnCells
可能会阻塞主线程(所有UI内容都会发生在主线程中),并且在您再次隐藏它之前,指示器永远不会出现

在这种情况下,请尝试在后台执行
applyActionOnCells
调用:

if(buttonIndex == 1) {
    [self.activityIndicator startAnimating];

    __block typeof(self) bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        ContactsTableViewController *contactTableView = [bself getContactsTVC];
        [contactTableView applyActionOnCells];

        dispatch_async(dispatch_get_main_queue(), ^{
            [bself.activityIndicator stopAnimating];

            // then go to rootViewController
            [bself.navigationController popToRootViewControllerAnimated:YES];
        });
    });
}

编辑:另请参见。

显示一些相关代码。如果您在实现自己的
UIActivityIndicator视图时遇到问题,您还可以查看
MBProgressHUD
,请编辑您的问题并向我们展示一些代码…非常有效。因此,我从中了解到,任何与UI相关的内容都必须在主线程上执行?如果它没有发生,那么主线程正忙于执行其他任务。对吗?没错。(官方引述如下)