如何按下按钮上传下一个视图或webview时显示活动指示器? [英] How show activity-indicator when press button for upload next view or webview?

查看:97
本文介绍了如何按下按钮上传下一个视图或webview时显示活动指示器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


当我点击按钮时,标题是点击这里放大然后我想在第一个视图上显示活动指示器,并在加载此视图时删除。

when i click on button which title is click here to enlarge then i want show activity indicator on the first view and remove when load this view.

但是我回去然后显示活动指示器,在此视图中显示。

but i go back then it show activity indicator which is shown in this view.

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

in first vie .m file i have use this code for action.

-(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 ];

        }} 

在上面的代码我有按钮动作我打电话下一个观点。现在我想在查看上传时显示/显示活动指示器。在下一个视图中,我有一个图像视图,其中我上传的图像我已经声明了一个也无效的活动指示器。怎么样?

in above code i have button action in which i call next view. Now i want show/display activity indicator when view upload. In next view i have a image view in which a image i upload i have declare an activity indicator which also not working. How do that?

推荐答案

Toro的建议提供了很好的解释和解决方案,但我只想提供另一种方法来实现这一点,因为这是我是怎么做的

Toro's suggestion offers a great explanation and solution, but I just wanted to offer up another way of achieving this, as this is how I do it.

正如Toro所说,

- (void) someFunction
{
    [activityIndicator startAnimation];

    // do computations ....

    [activityIndicator stopAnimation];  
}

以上代码无效,因为您没有给UI时间更新当您在当前运行的函数中包含activityIndi​​cator时。所以我和其他人做的就是把它分解成一个单独的线程,如下所示:

The above code will not work because you do not give the UI time to update when you include the activityIndicator in your currently running function. So what I and many others do is break it up into a separate thread like so:

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

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

    //Your computations

    [activityIndicator stopAnimating];

}

- (void) threadStartAnimating {
    [activityIndicator startAnimating];
}

祝你好运!
-Karoly

Good luck! -Karoly

这篇关于如何按下按钮上传下一个视图或webview时显示活动指示器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆