关于展示UIActivityIndi​​cator的另一个问题

时间:2011-01-15 13:16:53

标签: iphone objective-c

我的UIActivityIndi​​catorView在我的应用程序中的模拟器和其他3.0设备上运行良好。但是我发现它没有在新的iphone 4中旋转(或显示)。基本上我需要在点击按钮时显示活动指示器并在按钮点击事件完成时隐藏它。我正在使用下面的方法。

[NSThread detachNewThreadSelector: @selector(spinBegin) toTarget:self withObject:nil];

来自link。如上所述,它正确地将活动指示器旋转到除了4. *之外的所有。不确定原因。为了解决这个问题,我还采用了另一种方法(来自developer.apple.com)

`  (IBAction为)syncOnThreadAction:(ID)发送方

{

[self willStartJob];



[self performSelectorInBackground:

    @selector(inThreadStartDoJob:)

    withObject:theJobToDo

];

}

(无效)inThreadStartDoJob:(ID)theJobToDo

{

NSAutoreleasePool * pool;

NSString *          status;



pool = [[NSAutoreleasePool alloc] init];

assert(pool != nil);



status = [... do long running job specified by theJobToDo ...]

[self performSelectorOnMainThread:

    @selector(didStopJobWithStatus:)

    withObject:status

    waitUntilDone:NO

];



[pool drain];

}

`

问题在于,它显示acitivityVIewIndicator正确旋转(至少在模拟器上),但在停止后,顶部栏中的内置活动指示器(显示电池%等)仍在旋转。

我是目标C的新手。我完全完成了我的应用程序,但是对于这个愚蠢的事情。我意识到没有启动另一个线程就无法显示UIActivityView。最后,只是为了咆哮,我不明白他们为什么要这么复杂。我的意思是他们知道会出现这个问题,为什么不提供每个人都可以使用的示例代码,而不是推导出自己的解决方案。

最后,任何人都可以向我提供方向或一些示例代码。我真的很感激。我一直在寻找几个小时,但没有发现任何真正有用的东西!

1 个答案:

答案 0 :(得分:4)

为什么要在单独的线程上启动/停止指示器? 您发送到UIActivityIndi​​catorView 的任何方法必须在主(UI)线程上发送。

按下按钮发送的任何事件都将自动在主线程上运行。如果您使用后台线程来完成此过程,您可以执行以下操作:

- (IBAction)buttonPressed:(id)sender {
    // This runs on the main thread
    [activityIndicator startAnimating];
    [self performSelectorInBackground:@selector(inThreadStartDoJob:) withObject:theJobToDo];
}

- (void)inThreadStartDoJob:(id)theJobToDo {
    // Set up autorelease pool
    ...
    // Run your long-running action
    ...
    // Stop the spinner. Since we're in a background thread,
    // we need to push this to the UI Thread
    [activityIndicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:YES];
}

修改:关于顶部栏中的活动指示器(电池所在的位置),是否会根据网络活动自动启动/停止?

相关问题