[英]No calls occurring for observeValueForKeyPath for UIProgressView in Swift
With the help of this tutorial I was adding a progress bar to my view with Swift but the observer does not get any calls of itself. 在本教程的帮助下,我使用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
}
}
}
Taking a look on SWIFT - KVO in xcode 6 beta 6.. observeValueForKeyPath no longer called or observeValueForKeyPath not being called didn't help. 看一下SWIFT-xcode 6 beta 6中的KVO。不再调用 watchValueForKeyPath或未 调用 observeValueForKeyPath没有帮助。 Is UIProgressView not observable?
UIProgressView是否不可观察? The console shows after every page load
Finished navigating
but not once 1
. 每次页面加载
Finished navigating
后,控制台都会显示Finished navigating
但不会一次1
。
It should be 它应该是
self.webView.addObserver(self, forKeyPath: "estimatedProgress", options: .New, context: nil)
You observe the webview estimateProgress
,get the progress,then set the progress of the progressView 您观察webview的
estimateProgress
进度,获取进度,然后设置进度的进度
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.