我需要帮助。我很困惑为什么我的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)
}
}
答案 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
中输入此内容时你会得到这个结果:
检查THIS示例项目以获取更多信息。