我遗漏了一些关于 UIActivityIndicatorView
和 NSTimer
如何协同工作的信息。
我在 Interface Builder 中添加了这个 UIActivityIndicatorView
,设置如下:
UIWebView
实例化为 self.webV
,UIActivityIndicatorView
实例化为 self.indicator
。
我在实现文件中有如下代码:
-(void)viewDidLoad
{
[super viewDidLoad];
//Create UIWebView.
if (!self.webV)
{
self.webV = [[UIWebView alloc] init];
}
self.webV.delegate = self;
//Load web page.
NSString *baseURLString = @"myURL.com";
NSString *urlString = [baseURLString stringByAppendingPathComponent:@"myURL.com"];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0/2.0 target:self selector:@selector(timerLoad) userInfo:nil repeats:YES];
[self connectWithURL:urlString andBaseURLString:baseURLString];
}
-(void)timerLoad
{
if (!self.webV.loading)
{
[self.indicator stopAnimating];
}
else
{
[self.indicator startAnimating];
}
}
但是当 UIWebView
加载时,没有事件指示器显示。我做错了什么或遗漏了什么?
谢谢大家的帮助。
最佳答案
如果您反复调用启动/停止,我真的不确定 UIActivityIndicatorView
的行为应该是什么。我有理由相信它不应该以这种方式使用 :)
因此,即使您的问题特定于 NSTimer
和 UIActivityIndicatorView
,了解您应该以不同方式处理您的解决方案可能会有所帮助。
与其使用每半秒重复调用一次 [self.indicator startAnimating]
的计时器,不如使用 webview 委托(delegate)方法打开和关闭 UIActivityIndicatorView
.
-(void)viewDidLoad
{
[super viewDidLoad];
//Create UIWebView.
if (!self.webV)
{
self.webV = [[UIWebView alloc] init];
}
self.webV.delegate = self;
//Load web page.
NSString *baseURLString = @"myURL.com";
NSString *urlString = [baseURLString stringByAppendingPathComponent:@"myURL.com"];
//self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0/2.0 target:self selector:@selector(timerLoad) userInfo:nil repeats:YES];
[self connectWithURL:urlString andBaseURLString:baseURLString];
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
//start animating
[self.indicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
//stop animating
[self.indicator stopAnimating];
}
关于objective-c - 不了解 UIActivityIndicatorView 如何与 NSTimer 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13940894/