如何按下按钮上传下一个视图或webview时显示活动指示器?

ram*_*ram 4 xcode objective-c uiwebview uiview ios

我的第一个观点是这样的 当我点击按钮哪个标题点击这里放大然后我想要在第一个视图上显示活动指示器,并在加载此视图时删除. 在图像视图中从URL上传图像的第二个视图.

但我回去然后显示活动指示器,在此视图中显示. 带活动指标的第一个视图

在第一个vie .m文件中,我使用此代码进行操作.

-(IBAction)btnSelected:(id)sender{
UIButton *button = (UIButton *)sender;
int whichButton = button.tag;
NSLog(@"Current TAG: %i", whichButton);
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[spinner setCenter:CGPointMake(160,124)]; 
[self.view addSubview:spinner]; 
[spinner startAnimating];

if(whichButton==1)
{
    [spinner stopAnimating];

    first=[[FirstImage alloc]init];
    [self.navigationController pushViewController:first animated:YES];
    [spinner hidesWhenStopped ];

        }} 
Run Code Online (Sandbox Code Playgroud)

在上面的代码我有按钮动作,我在其中调用下一个视图.现在我想在查看上传时显示/显示活动指示器.在下一个视图中,我有一个图像视图,其中我上传的图像我已经声明了一个也不起作用的活动指示器.怎么样?

Kar*_*y S 9

Toro的建议提供了很好的解释和解决方案,但我只是想提供另一种方法来实现这一目标,因为这就是我的工作方式.

正如托罗所说,

- (void) someFunction
{
    [activityIndicator startAnimation];

    // do computations ....

    [activityIndicator stopAnimation];  
}
Run Code Online (Sandbox Code Playgroud)

上面的代码不起作用,因为当您在当前运行的函数中包含activityIndi​​cator时,不会给UI时间更新.所以我和其他许多人所做的就是把它分解成一个单独的线程,如下所示:

- (void) yourMainFunction {
    activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

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

    //Your computations

    [activityIndicator stopAnimating];

}

- (void) threadStartAnimating {
    [activityIndicator startAnimating];
}
Run Code Online (Sandbox Code Playgroud)

祝好运!-Karoly