基本的iOS浏览器代码

时间:2015-08-10 05:42:34

标签: ios swift mobile browser uiwebview

我需要帮助。我很困惑为什么我的iOS Swift代码不起作用。我想创建一个基本浏览器,以便将地址放在textfield上,并显示在我的UIWebView上。但是当我按下“运行模拟”按钮时,它会将我带回编码窗口。有谁知道为什么?

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func urlField(sender: UITextField) {
        let url = NSURL(string:"http://facebook.com")
        let request = NSURLRequest(URL:url!)
        webView.loadRequest(request)
    }

}

1 个答案:

答案 0 :(得分:0)

这是一个非常基本的例子。

import UIKit
import WebKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var webView: UIWebView!
    @IBOutlet weak var textField: UITextField!

    //when you press search button webView will search what you have entered into textField.
    @IBAction func Search(sender: AnyObject) {
        var text = textField.text
        var url = NSURL(string: text)
        var req = NSURLRequest(URL:url!)
        self.webView!.loadRequest(req)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.textField.delegate = self
    }
}

当您在textField:http://facebook.com

中输入此内容时

你会得到这个结果:

enter image description here

检查THIS示例项目以获取更多信息。