在 Swift 中使用 UIActivityIndi​​catorView 和 UIWebView [英] Using UIActivityIndicatorView with UIWebView in Swift

查看:89
本文介绍了在 Swift 中使用 UIActivityIndi​​catorView 和 UIWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在将 url 加载到 WebView 时向用户显示我的应用程序中的活动指示器视图.我试过玩弄 activity.startAnimating/activity.stopAnimating 并尝试将它们放在函数等中,但没有任何运气.

I'm trying to display an Activity Indicator View in my app to users while a url is being loaded into a WebView. I've tried toying with activity.startAnimating/activity.stopAnimating and tried placing them in functions, etc. but have not had any luck.

我能得到的最好的是 Activity Indicator 显示和动画,但一旦我的 url 被加载就不会停止动画或隐藏,所以它继续在网页顶部旋转.

The best I have been able to get is the Activity Indicator to appear and animate, but then not stop animating or hide once my url is loaded, so it continues spinning on top of the web page.

在其他情况下,当试图在activity.startAnimating 中移动时,我遇到了Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)"问题.我在属性检查器中选中了停止时隐藏",我知道我的 url 是有效的,并且我已经为 Interface Builder Elements 创建了 IBOutlets.

In other situations, when trying to move around activity.startAnimating, I have encountered the "Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)" issue. I have "Hides when Stopped" checked in the attributes inspector, I know that my url is valid, and I have created IBOutlets for the Interface Builder Elements.

忍受我;我对 Swift 比较陌生.这是我的代码:

Bear with me; I'm relatively new to Swift. Here's my code:

class HighchartsController: UIViewController {

@IBOutlet weak var HighchartsView: UIWebView!

@IBOutlet weak var activity: UIActivityIndicatorView!

@IBOutlet weak var saveButton: UIBarButtonItem!

@IBOutlet weak var highchartsMenu: UIBarButtonItem!

override func viewDidLoad()
{
    super.viewDidLoad()

    if self.revealViewController() != nil {
        highchartsMenu.target = self.revealViewController()
        highchartsMenu.action = "revealToggle:"

        loadAddress()
    }
 }

func loadAddress() {

    let url = NSURL (string: "http://google.com/flights")
    let request = NSURLRequest (URL: url!)
    HighchartsView.loadRequest(request)
    println("Webpage Loaded Successfully")
 }
}

我尝试使用不同的功能,例如

And I have tried using different functions such as

webViewDidStartLoad(_ :UIWebView){

    activity.startAnimating()
    NSLog("Webview load has started")

}
webViewDidFinishLoad(_ :UIWebView){

    activity.stopAnimating()
    NSLog("Webview load had finished")

}

推荐答案

首先,我没有看到 UIWebView 的委托.意识到您与委派流程相关的行为.

First of all, i don't see delegation of UIWebView. Realize your behaviour related to delegation processes.

UIWebViewDelegate 有四个方法,但这种方式只使用三个:

UIWebViewDelegate has four methods, but use for this way just three:

func webViewDidStartLoad(_ webView: UIWebView) // show indicator    
func webViewDidFinishLoad(_ webView: UIWebView) // hide indicator
func webView(_ webView: UIWebView, didFailLoadWithError error: Error) // hide indicator

斯威夫特 3

func webViewDidStartLoad(webView: UIWebView!) // show indicator
func webViewDidFinishLoad(webView: UIWebView!) // hide indicator
func webView(webView: UIWebView!, didFailLoadWithError error: NSError!) // hide indicator

这篇关于在 Swift 中使用 UIActivityIndi​​catorView 和 UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆