gpt4 book ai didi

ios - Swift 中没有调用 UIProgressView 的 observeValueForKeyPath

转载 作者:行者123 更新时间:2023-11-28 13:00:59 25 4
gpt4 key购买 nike

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

//  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 calledobserveValueForKeyPath not being called没有帮助。 UIProgressView 是不可观察的吗?控制台在每次页面加载后显示 Finished navigating 但不是一次 1

最佳答案

应该是

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

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

关于ios - Swift 中没有调用 UIProgressView 的 observeValueForKeyPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33851249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com