objective-c - 不了解 UIActivityIndi​​catorView 如何与 NSTimer 一起工作

标签 objective-c ios uiwebview nstimer uiactivityindicatorview

我遗漏了一些关于 UIActivityIndi​​catorViewNSTimer 如何协同工作的信息。

我在 Interface Builder 中添加了这个 UIActivityIndi​​catorView,设置如下:

UIActivityIndicatorView UIActivityIndicatorView settings

UIWebView 实例化为 self.webVUIActivityIndi​​catorView 实例化为 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 加载时,没有事件指示器显示。我做错了什么或遗漏了什么?

谢谢大家的帮助。

最佳答案

如果您反复调用启动/停止,我真的不确定 UIActivityIndi​​catorView 的行为应该是什么。我有理由相信它不应该以这种方式使用 :)

因此,即使您的问题特定于 NSTimerUIActivityIndi​​catorView,了解您应该以不同方式处理您的解决方案可能会有所帮助。

与其使用每半秒重复调用一次 [self.indicator startAnimating] 的计时器,不如使用 webview 委托(delegate)方法打开和关闭 UIActivityIndi​​catorView .

       -(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 - 不了解 UIActivityIndi​​catorView 如何与 NSTimer 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13940894/

相关文章:

ios - 在表格 View 单元格中编辑文本字段的最佳方法

iphone - UIMotionEffect 在应用于 View 时不起作用

ios - 如何在表格 View 单元格上加载不同的图像

ios - 从 OSX 复制 p12 证书,应用程序在调用 Web API 时将使用该证书

ios - 将 UIWebView 的(通过 Web 服务加载)<img> 标记源替换为本地(缓存)源

ios - 如何使用webview只显示网页的一部分

ios - 适用于 iOS 的即时 alpha 或魔棒工具

ios - UITableViewCell 中的 UIButton 在 iOS 5x 中没有响应但在 iOS 6 中响应

ios - NSDateFormatter for rails 4.0 created_at

ios - 如何在 Swift 中以编程方式更改 UIWebView 方向