2017-05-27 105 views
0

我的網頁在我的應用程序中加載後,我的活動指示器停止並隱藏時出現問題。這裏是我的代碼與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") 
    } 

} 

回答

-1

它應該是不夠的,只是使用activityIndicatorwebViewDidStartLoadwebViewDidFinishLoad

不需要隱藏它,只要確保它具有hidesWhenStopped屬性爲true。通過將其設置在您的Storyboard中或在webViewDidFinishLoad內設置activityIndicator.activityIndicator.hidesWhenStopped

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

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

看看UIWebViewDelegate,如你想使用它的功能,但不繼承和分配它的類。

雖然你應該看看MVC,你可以讓你的視圖控制器成爲你的代表。

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

webViewDidStartLoadwebViewDidFinishLoad職能應該按預期工作後。

相關問題