UIWebView swift
//
// ViewController.swift
// UILabelTest
//
// Created by mac on 15/6/23.
// Copyright (c) 2015年 fangyuhao. All rights reserved.
//
import UIKit
class ViewController: UIViewController,UIWebViewDelegate,UITextFieldDelegate{
@IBOutlet weak var btngo: UIButton!
@IBOutlet weak var webview: UIWebView!
@IBOutlet weak var txturl: UITextField!
var loadIndicator:UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
self.webview.delegate = self
loadIndicator = UIActivityIndicatorView(frame: CGRectMake(100, 100, 32, 32))
loadIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
self.view.addSubview(loadIndicator)
txturl.delegate = self
// var urlobj = NSURL(fileURLWithPath: "www.baidu.com")
// var request = NSURLRequest(URL: urlobj!)
// webview.loadRequest(request)
// webview.loadRequest(NSURLRequest(URL: NSURL(string: "www.baidu.com")!))
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
txturl.resignFirstResponder()
println("url Changed!")
var url = txturl.text
loadUrl(url)
return true
}
func loadUrl(url:String){
var urlobj = NSURL(string: url)
var request = NSURLRequest(URL: urlobj!)
webview.loadRequest(request)
}
@IBAction func goClicked(sender: UIButton) {
txturl.resignFirstResponder()
var url = txturl.text
loadUrl(url)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func webViewDidStartLoad(webView: UIWebView) {
loadIndicator.startAnimating()
}
func webViewDidFinishLoad(webView: UIWebView) {
loadIndicator.stopAnimating()
}
func webView(webView: UIWebView, didFailLoadWithError error: NSError) {
var alertview = UIAlertView()
alertview.title = "出错"
alertview.message = error.localizedDescription
alertview.addButtonWithTitle("确定")
alertview.show()
}
}