Swift 中没有调用 UIProgressView 的 observeValueForKeyPath

No calls occurring for observeValueForKeyPath for UIProgressView in Swift

this tutorial 的帮助下,我使用 Swift 在我的视图中添加了一个进度条,但是观察者自身没有收到任何调用。

//  ViewController.swift

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {

    var webView: WKWebView?

    @IBOutlet dynamic var progressView: UIProgressView!

    override func viewDidLoad() {
        super.viewDidLoad()

        setupBrowser()

        self.addObserver(self, forKeyPath: "estimatedProgress", options: .New, context: nil)

        let website = "https://google.com"
        let site = NSURL(string: website)

        loadURLRequest(NSURLRequest(URL: site!))
    }

    func setupBrowser() {
        let wkWebView = WKWebView(frame: view.bounds)
        wkWebView.navigationDelegate = self
        webView = wkWebView

        let wkProgressView = UIProgressView(frame: view.bounds)
        progressView = wkProgressView

        self.view.insertSubview(wkWebView, belowSubview: wkProgressView)
    }

    func loadURLRequest(request: NSURLRequest) {
        webView?.loadRequest(request)
    }

    func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
        print("Finished navigating")
    }

    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        print(1)
        if (keyPath == "estimatedProgress") {
            // TODO
        }
    }

}

查看 SWIFT - KVO in xcode 6 beta 6.. observeValueForKeyPath no longer called or observeValueForKeyPath not being called 没有帮助。 UIProgressView 是不可观察的吗?控制台在每次页面加载后显示 Finished navigating 但不会显示一次 1.

应该是

 self.webView.addObserver(self, forKeyPath: "estimatedProgress", options: .New, context: nil)

你观察webviewestimateProgress,获取进度,然后设置progressView的进度