我试图制作一个networkActivityIndicator(在SWIFT中),但没有发生任何事情,我做了一些测试,但没有。此ActivityIndicator通常链接到WebView。
class ViewController: UIViewController, UISearchBarDelegate, UIWebViewDelegate {
@IBOutlet weak var WebView: UIWebView!
@IBOutlet weak var SearchBar: UISearchBar!
//WebView
func WebViewDidStartLoad(WebView : UIWebView) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
println("Chargement")
}
func WebViewDidFinishLoad(WebView : UIWebView) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
println("Fin chargement")
}
//SearchBar
func searchBarSearchButtonClicked(SearchBar: UISearchBar!) {
SearchBar.resignFirstResponder()
var text = SearchBar.text
text = text.stringByReplacingOccurrencesOfString(" ", withString: "+");
var url = NSURL(string: "https://www.google.fr/#q=".stringByAppendingString(text));
var req = NSURLRequest(URL:url!)
WebView.delegate = self
self.WebView!.loadRequest(req)
}
答案 0 :(得分:5)
以下是完整的工作代码:
import UIKit
import WebKit
class ViewController: UIViewController, UISearchBarDelegate, UIWebViewDelegate {
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var webView: UIWebView!
func searchBarSearchButtonClicked(searchBar: UISearchBar!) {
searchBar.resignFirstResponder()
var text = searchBar.text
text = text.stringByReplacingOccurrencesOfString(" ", withString: "+");
var url = NSURL(string: "http://google.com/search?q=".stringByAppendingString(text));
var req = NSURLRequest(URL:url!)
self.webView!.loadRequest(req)
}
override func viewDidLoad() {
super.viewDidLoad()
self.searchBar.delegate = self
webView.delegate = self
}
func webViewDidStartLoad(webView: UIWebView){
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
println("Chargement")
}
func webViewDidFinishLoad(webView: UIWebView){
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
println("Fin chargement")
}
}
希望它有所帮助。
答案 1 :(得分:0)
在swift 4.1中,替换下面的
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
与
UIApplication.shared.isNetworkActivityIndicatorVisible = true