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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6829279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:52:28  来源:igfitidea点击:

How show activity-indicator when press button for upload next view or webview?

objective-ciosxcodeuiviewuiwebview

提问by ram

My first view is looks like  this 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. second view in which image is upload from URL in image view.

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

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

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

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

在第一个 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 ];

        }} 

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?

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

回答by Karoly S

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 的建议提供了很好的解释和解决方案,但我只是想提供另一种实现方式,因为我就是这样做的。

As Toro said,

正如托罗所说,

- (void) someFunction
{
    [activityIndicator startAnimation];

    // do computations ....

    [activityIndicator stopAnimation];  
}

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:

上面的代码将不起作用,因为当您在当前运行的函数中包含 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];
}

Good luck! -Karoly

祝你好运!-卡罗利

回答by AechoLiu

[self.navigationController pushViewController:first animated:YES];

Generally, when you push a view controller into navigation controller, it will invoke the -(void)viewWillAppear:and -(void)viewDidAppear:methods. You can add activity indicator view inside the viewWillAppear:and call startAnimation of indicator view. You CANNOTinvoke startAnimationand stopAnimationat the same time. For example,

通常,当您将视图控制器推送到导航控制器时,它会调用-(void)viewWillAppear:-(void)viewDidAppear:方法。您可以在其中添加活动指示器视图viewWillAppear:并调用指示器视图的 startAnimation。您不能同时调用startAnimationstopAnimation。例如,

- (void)viewWillAppear:(BOOL)animated 
{
    [aIndicatorView startAnimation];

    // do somethings ....

    [aIndicatorView stopAnimation];  
}

Because the startAnimationand stopAnimationare under the same time, then no animation will show.

因为startAnimationstopAnimation是同时下的,那么就不会显示动画了。

But if you invoke startAnimationin -(void)viewWillAppear:and invoke stopAnimationin another message, like followings.

但是,如果你调用startAnimation-(void)viewWillAppear:并调用stopAnimation在另一条消息,像以下。

- (void)viewWillAppear:(BOOL)animated
{
    [aIndicatorView startAnimation];

    // do somethings... 
}

- (void)viewDidAppear:(BOOL)animated
{
    [aIndicatorView stopAnimation];
}

Because viewWillAppear:and viewDidAppear:are invoked with different event time, the activity indicator view will work well.

因为viewWillAppear:viewDidAppear:是用不同的事件时间调用的,所以活动指示器视图可以很好地工作。

Or, you can do something like followings:

或者,您可以执行以下操作:

- (void)viewWillAppear:(BOOL)animated 
{
    [aIndicatorView startAnimation];

    // Let run loop has chances to animations, others events in run loop queue, and ... etc. 
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]];

    // do somethings ....

    [aIndicatorView stopAnimation];
}

The above example is a badexample, because it invokes two or more animations in the -runUntilDate:. But it will let the activity indicator view work.

上面的例子是一个bad例子,因为它在-runUntilDate:. 但它会让活动指示器视图工作。

回答by Praveen S

Create a webview. Add a activity indicator to the webview. If you are loading a image via url into the webview then implement the webview delegate methods. Once the url is loaded then stopanimating the activity indicator.

创建一个网络视图。向 web 视图添加活动指示器。如果您通过 url 将图像加载到 webview 中,则实现 webview 委托方法。加载 url 后,停止对活动指示器进行动画处理。

Let me know which step you are not able to implement.

让我知道您无法实施哪个步骤。