网页加载后停止并隐藏 activity 指示器
Stop and hide activity indicator when webpage has loaded
我的 activity 指示器在我的网页加载到我的应用程序后停止并隐藏时遇到问题。这是我删除了 activityIndicator.stopAnimating() 的代码,因为我把它放在我的代码中的任何地方都会阻止指标启动。
这是我的代码:
import UIKit
class AppointmentsViewController: UIViewController {
@IBOutlet weak var myWebView: UIWebView!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
if Reachability.isConnectedToNetwork(){
webViewDidStartLoad(webView: myWebView)
let url = URL(string: "https://www.posebeautysalon.com/appointment")!
myWebView.loadRequest(URLRequest(url: url))
}
else{
print("Internet Connection not Available!")
let alertController = UIAlertController(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", preferredStyle: .alert)
// Create the actions
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
UIAlertAction in
NSLog("OK Pressed")
}
// Add the actions
alertController.addAction(okAction)
// Present the controller
self.present(alertController, animated: true, completion: nil)
}
}
func webViewDidStartLoad(webView: UIWebView) {
activityIndicator.startAnimating()
NSLog("The webview is starting to load")
}
func webViewDidFinishLoad(webView: UIWebView) {
activityIndicator.stopAnimating()
activityIndicator.isHidden=true;
NSLog("The webview is done loading")
}
}
在webViewDidStartLoad
和webViewDidFinishLoad
中使用你的activityIndicator
应该就足够了。
不需要隐藏它,只要确保它的 hidesWhenStopped
属性为真。通过在您的 Storyboard 中设置它或在 webViewDidFinishLoad
.
中设置 activityIndicator.activityIndicator.hidesWhenStopped
func webViewDidStartLoad(_ webView : UIWebView) {
activityIndicator.startAnimating()
}
func webViewDidFinishLoad(_ webView : UIWebView) {
activityIndicator.stopAnimating()
}
看看 UIWebViewDelegate
,因为您正在尝试使用它的功能,但没有继承和分配它的 class。
尽管您应该研究 MVC,但您可以让视图控制器成为您的委托。
class AppointmentsViewController: UIViewController, UIWebViewDelegate {
...
func viewDidLoad() {
super.viewDidLoad()
myWebView.delegate = self
...
}
}
在那之后,您的 webViewDidStartLoad
和 webViewDidFinishLoad
函数应该会按预期工作。
我的 activity 指示器在我的网页加载到我的应用程序后停止并隐藏时遇到问题。这是我删除了 activityIndicator.stopAnimating() 的代码,因为我把它放在我的代码中的任何地方都会阻止指标启动。
这是我的代码:
import UIKit
class AppointmentsViewController: UIViewController {
@IBOutlet weak var myWebView: UIWebView!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
if Reachability.isConnectedToNetwork(){
webViewDidStartLoad(webView: myWebView)
let url = URL(string: "https://www.posebeautysalon.com/appointment")!
myWebView.loadRequest(URLRequest(url: url))
}
else{
print("Internet Connection not Available!")
let alertController = UIAlertController(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", preferredStyle: .alert)
// Create the actions
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
UIAlertAction in
NSLog("OK Pressed")
}
// Add the actions
alertController.addAction(okAction)
// Present the controller
self.present(alertController, animated: true, completion: nil)
}
}
func webViewDidStartLoad(webView: UIWebView) {
activityIndicator.startAnimating()
NSLog("The webview is starting to load")
}
func webViewDidFinishLoad(webView: UIWebView) {
activityIndicator.stopAnimating()
activityIndicator.isHidden=true;
NSLog("The webview is done loading")
}
}
在webViewDidStartLoad
和webViewDidFinishLoad
中使用你的activityIndicator
应该就足够了。
不需要隐藏它,只要确保它的 hidesWhenStopped
属性为真。通过在您的 Storyboard 中设置它或在 webViewDidFinishLoad
.
activityIndicator.activityIndicator.hidesWhenStopped
func webViewDidStartLoad(_ webView : UIWebView) {
activityIndicator.startAnimating()
}
func webViewDidFinishLoad(_ webView : UIWebView) {
activityIndicator.stopAnimating()
}
看看 UIWebViewDelegate
,因为您正在尝试使用它的功能,但没有继承和分配它的 class。
尽管您应该研究 MVC,但您可以让视图控制器成为您的委托。
class AppointmentsViewController: UIViewController, UIWebViewDelegate {
...
func viewDidLoad() {
super.viewDidLoad()
myWebView.delegate = self
...
}
}
在那之后,您的 webViewDidStartLoad
和 webViewDidFinishLoad
函数应该会按预期工作。