5

自 iOS 6 以来一直有这个问题。我真的不知道发生了什么变化会导致这种行为。这曾经在 5 中完美运行。现在活动指示器没有及时出现或根本没有出现。任何帮助将非常感激。

-(void)myMethod
{
    UIView *currentTitleView = [[self navigationItem] titleView];


    // Create an activity indicator and start it spinning in the nav bar
    UIActivityIndicatorView *aiview = [[UIActivityIndicatorView alloc]     initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

    [[self navigationItem] setTitleView:aiview];
    [aiview startAnimating];

    // Start of Block code
    void (^block)(arg1, arg2) =
    ^(arg1, arg2)
    {
       block code;
       [aiview stopAnimating];
       [[self navigationItem] setTitleView:currentTitleView];
    };
// End of Block code




}
4

1 回答 1

0

听起来像是myMethod从后台线程调用的。作为一般规则,与 UIKit 元素(包括UIActivityIndicatorView)的所有交互都应始终在主线程上完成。尝试使用 GCD 将 UI 代码移动到主队列(也称为主线程)。

-(void)myMethod {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIView *currentTitleView = [[self navigationItem] titleView];

        // Create an activity indicator and start it spinning in the nav bar
        UIActivityIndicatorView *aiview = [[UIActivityIndicatorView alloc]     initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

        [[self navigationItem] setTitleView:aiview];
        [aiview startAnimating];
    });

    // Start of Block code
    void (^block)(arg1, arg2) =
    ^(arg1, arg2)
    {
       block code;

        dispatch_async(dispatch_get_main_queue(), ^{
           [aiview stopAnimating];
           [[self navigationItem] setTitleView:currentTitleView];
        });
    };
// End of Block code
}
于 2013-04-12T19:58:33.033 回答