活动指示器 webView Xcode 5 IOS 7

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21660531/
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-09-15 04:38:19  来源:igfitidea点击:

Activity Indicator webView Xcode 5 IOS 7

iosxcodeios7android-activityxcode5

提问by Johan Enstam

Hi i have a problem with creating a Activity Indicator for my webView, this is the code im using.

嗨,我在为我的 webView 创建活动指示器时遇到问题,这是我使用的代码。

ViewController.h

视图控制器.h

@interface ViewController :UIViewController {
IBOutlet UIWebView *webview;
IBOutlet UIActivityIndicatorView*activityind;
NSTimer *timer; 
}
@end

ViewController.m

视图控制器.m

@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[webview loadRequest:[NSURLRequest requestWithURL:
[NSURLURLWithString:@"http://www.twitter.com/geekylemon"]]];
[webview addSubview:activityind];
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0)
target:self
selector:@selector(loading)
userInfo:nil
repeats:YES];

}

-(void)loading {  
if (!webview.loading)
    [activityind stopAnimating];
else
    [activityind startAnimating];
} 
@end

采纳答案by Bamsworld

There is no need to add the activity indicator as a subview of your webview, just position it where you want in the UI in interface builder and connect it to your outlet.

无需将活动指示器添加为您的 webview 的子视图,只需将其放置在界面构建器的 UI 中您想要的位置并将其连接到您的插座。

Also make sure the hidesWhenStoppedproperty is set to YES. You can do this in the viewDidLoadmethod of your controller.

还要确保hidesWhenStopped属性设置为YES。您可以在控制器的viewDidLoad方法中执行此操作。

Like this - (working with your existing code)

像这样 - (使用您现有的代码

- (void)viewDidLoad {
     [super viewDidLoad];
     activityind.hidesWhenStopped = YES;
     [webview loadRequest:[NSURLRequest requestWithURL:[NSURLURLWithString:@"http://www.twitter.com/geekylemon"]]];
     timer = [NSTimer scheduledTimerWithTimeInterval:0.5
          target:self
          selector:@selector(loading)
          userInfo:nil
          repeats:YES];
}

Also remember to invalidate the timer when you stop animating the activity indicator.

还要记住在停止为活动指示器设置动画时使计时器无效。

This tutorial I found on youTube explains very well exactly what you are asking - iPhone Programming - UIActivityIndicator with a WebView

我在 youTube 上找到的这个教程很好地解释了你在问什么 - iPhone 编程 - UIActivityIndi​​cator with a WebView

回答by Rhys

i would suggest using the delegate methods of the UIWebView to stop and start your activity indicator.

我建议使用 UIWebView 的委托方法来停止和启动您的活动指示器。

webViewDidStartLoad and webViewDidFinishLoad should be exactly what your looking for.

webViewDidStartLoad 和 webViewDidFinishLoad 应该正是您想要的。

in view did load set your view controller as the delegate like so:

在视图中确实加载将您的视图控制器设置为委托,如下所示:

webview.delegate = self;

then in your .h file specify that your viewController conforms to the UIWebViewDelegate protocol by changing your .h file to this:

然后在您的 .h 文件中,通过将 .h 文件更改为以下内容来指定您的 viewController 符合 UIWebViewDelegate 协议:

@interface ViewController :UIViewController <UIWebViewDelegate> {
IBOutlet UIWebView *webview;
IBOutlet UIActivityIndicatorView*activityind;
NSTimer *timer; 
}
@end

Then in your .m file use the delegate methods:

然后在您的 .m 文件中使用委托方法:

-(void)webViewDidStartLoad:(UIWebView *)webView{

    [activityind startAnimating];

}

-(void)webViewDidFinishLoad:(UIWebView *)webView{

    [activityind stopAnimating];
}