objective-c 加载 UIWebView 时如何实现 UIActivityIndicatorView?(iPhone 对象)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1311088/
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
How implement a UIActivityIndicatorView when the UIWebView is Loading? (iPhone ObjC)
提问by Matt Long
I want to know how to implement an activityIndicator in a WebView based app, I wrote the following code but the indicator does not appear.
我想知道如何在基于 WebView 的应用程序中实现一个 activityIndicator,我编写了以下代码但该指标没有出现。
The webview load file locally, so it load very fast, but when it load an external page it load slow and I need the indicator...
webview在本地加载文件,因此加载速度非常快,但是当加载外部页面时加载速度很慢,我需要指示器...
FirstViewController.h
第一视图控制器.h
#import <UIKit/UIKit.h>
@interface FirstViewController :
UIViewController <UIWebViewDelegate>{
IBOutlet UIWebView *webview1;
NSURL *urlLocation;
IBOutlet UIActivityIndicatorView *m_activity;
}
@property (nonatomic, retain) UIActivityIndicatorView *m_activity;
- (IBAction)searchbutton:(id)sender;
- (IBAction)home:(id)sender;
@end
FirstViewController.m
第一视图控制器.m
#import "FirstViewController.h"
@implementation FirstViewController
@synthesize m_activity;
// viewWillAppear loads every time younopen up this View
- (void)viewWillAppear:(BOOL)animated {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
urlLocation = [NSURL fileURLWithPath:filePath];
[webview1 loadRequest:[NSURLRequest requestWithURL:urlLocation]];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
//Initialization code
m_activity = nil;
}
return self;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
m_activity.hidden= TRUE;
[m_activity stopAnimating];
NSLog(@"Web View started loading...");
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
m_activity.hidden= FALSE;
[m_activity startAnimating];
NSLog(@"Web View Did finish loading");
}
回答by Matt Long
Why are you setting your activity indicator to nil in your init?
为什么在 init 中将活动指示器设置为 nil?
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
//Initialization code
m_activity = nil;
}
return self;
}
The call to super initialized your indicator from your XIB (assuming you connected it to your outlet in IB), but then you are setting the reference to nil after it's been initialized. Remove that line. Then go back into interface builder and set the "Hide when stopped" checkbox. Now you can simplify your code that displays the indicator:
对 super 的调用从您的 XIB 初始化了您的指标(假设您将其连接到您在 IB 中的插座),但是您在初始化后将引用设置为 nil。删除该行。然后返回界面构建器并设置“停止时隐藏”复选框。现在您可以简化显示指标的代码:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[m_activity stopAnimating];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[m_activity startAnimating];
}
The "Hide when stopped" causes the indicator to hide when you stop it from animating.
“停止时隐藏”会导致指示器在您停止动画时隐藏。
回答by Daniel
Whats the issue here, the code you posted above should work, except that you dont initialize the indicator anywhere (maybe you do in viewDidLoad) but the code shown above should work given that the indicator was initialized correctly and u set the webview d elegate to the view controller there, I have it working on some of my apps where i use webviews and indicators to indicate when its loading...
这里有什么问题,你上面发布的代码应该可以工作,除了你没有在任何地方初始化指标(也许你在 viewDidLoad 中这样做)但是上面显示的代码应该可以工作,因为指标已经正确初始化并且你将 webview delegate 设置为那里的视图控制器,我让它在我的一些应用程序上工作,我使用 webviews 和指示器来指示它何时加载......
回答by Mazed
UIWebView.loading property can also be used.
也可以使用 UIWebView.loading 属性。
Apple's doc: @property(nonatomic, readonly, getter=isLoading) BOOL loading Description A Boolean value indicating whether the receiver is done loading content. (read-only) If YES, the receiver is still loading content; otherwise, NO.
Apple 的文档:@property(nonatomic, readonly, getter=isLoading) BOOL loading 描述 一个布尔值,指示接收器是否已完成加载内容。(只读)如果是,则接收方仍在加载内容;否则,否。
In iOS6 it looks like Apple has fixed some issues with this property as well. http://code-gotcha.blogspot.fi/2012/08/uiwebviewloading-in-ios-6-fixed.html
在 iOS6 中,Apple 似乎也修复了此属性的一些问题。http://code-gotcha.blogspot.fi/2012/08/uiwebviewloading-in-ios-6-fixed.html

