【问题标题】:Stop and hide activity indicator when webpage has loaded网页加载后停止并隐藏活动指示器
【发布时间】:2017-05-27 10:56:09
【问题描述】:

在我的应用程序中加载我的网页后,我无法让我的活动指示器停止并隐藏。这是我删除了 activityIndi​​cator.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")
    }

}

【问题讨论】:

    标签: ios swift webview uiactivityindicatorview


    【解决方案1】:

    看看UIWebViewDelegate,你正在尝试使用它的功能,但没有继承和分配它的类。

    虽然您应该查看MVC,但您可以让您的视图控制器成为您的代表。

    class AppointmentsViewController: UIViewController, UIWebViewDelegate {
      ...
      func viewDidLoad() {
        super.viewDidLoad()
        myWebView.delegate = self
        ...
      }
    }
    

    之后,您的 webViewDidStartLoadwebViewDidFinishLoad 函数应该可以按预期工作。

    【讨论】:

      【解决方案2】:

      只需在webViewDidStartLoadwebViewDidFinishLoad 中使用activityIndicator 就足够了。

      无需隐藏它,只需确保它的hidesWhenStopped 属性为true。通过在 Storyboard 中或在 webViewDidFinishLoad 中设置 activityIndicator.activityIndicator.hidesWhenStopped

      func webViewDidStartLoad(_ webView : UIWebView) {
          activityIndicator.startAnimating()
      }
      
      func webViewDidFinishLoad(_ webView : UIWebView) {
          activityIndicator.stopAnimating()
      }
      

      【讨论】:

        猜你喜欢
        • 2019-02-05
        • 1970-01-01
        • 2016-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-21
        相关资源
        最近更新 更多