0

私は現在、アプリを開くと特定のURLにリンクしているアプリケーションを持っています。これは実際に読み込んでいるときにwebViewを開くまでに時間がかかることがあるので、WebViewが読み込まれるたびにアクティビティインジケータを表示します。私ViewController.swiftに次のコード:UIActivityIndi​​catorが表示されたら消える方法?

class ViewController: UIViewController, UIWebViewDelegate { 


@IBOutlet weak var webView: UIWebView! 

@IBOutlet weak var activityIndicator: UIActivityIndicatorView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    webView.delegate = self 

    let url = URL(string: "url to site") 
    webView.loadRequest(URLRequest(url: url!) 
} 

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

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

活動の指標には、最初から現れるが、それはかつてのWebView負荷を消え、その永遠にとどまるものではありません。

答えて

0

代わりにこれを試してみてください、あなたは関数で、下線を追加する必要があります。

override func viewDidLoad() { 
    super.viewDidLoad() 

    webView.delegate = self 

    let url = URL(string: "your URL") 
    webView.loadRequest(URLRequest(url: url!)) 
} 

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool{ 

    activityIndicator.startAnimating() 
    return true 
} 

func webViewDidStartLoad(_ webView: UIWebView){ 

    activityIndicator.startAnimating() 
} 

func webViewDidFinishLoad(_ webView: UIWebView){ 

    activityIndicator.stopAnimating() 
} 

func webView(_ webView: UIWebView, didFailLoadWithError error: Error){ 

    activityIndicator.stopAnimating() 
} 
0

あなたのコードを整理する方法はちょっと混乱します。 func@IBActionの内部に宣言しているようです。この場合、これは機能しません。

問題は、構文の問題です

class ViewController: UIViewController, UIWebviewDelegate { 
    override func viewDidLoad() { 
     ... 
     webView.delegate = self 

     // You should add this to your Storyboard, above the webview instead of here in the code 
     activityIndicator.center = self.view.center 
     activityIndicator.hidesWhenStopped = true 
     activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray 
     // I forget the actual method name - look it up 
     view.insertSubview(activityIndicator, above: webView) 
    } 

    @IBAction func openButton(_ sender: Any) { 
     let url = URL(string: "websiteURL") 
     webView.loadRequest(URLRequest(url: url!)) 
    } 

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

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

    // You'll also want to add the "didFail" method 
} 
+0

ありがとうございました。ありがとうございました。ありがとうございました。私はあなたに感謝したいと思ったら、私が探しているものをより良く表示するために質問を更新しました。 – athill16

+0

@ athill16 'hidesWhenStopped'は' didFail'デリゲートメソッドを実装していると思われるので、 'true'であることを確認します。 'webViewDidFinishLoad'にブレークポイントを置くと、実際にここで停止しますか? –

+0

ブレークポイントを追加してもトリガーはかかりませんでしたが、問題は実際にはほとんどの問題を修正した構文であったため、ポストで更新します。私の唯一の質問はdidFailメソッドが実際に何をしているかです。これは、人の電話接続が非常に悪いためにWebViewが読み込めない場合ですか? – athill16

関連する問題