ios - How do I connect the search bar with the uiwebview in Xcode 6 using Swift? -
I'm new to coding with swift and I need help: how do I add search bar to my web view When I type a search, does it appear in Google?
My code is up to now:
Import UIKit Class ViewController: UIViewController {@Iboutlet var Webview: UIWebView! Request override func viewDidLoad () {url = NSURL (string: "http://google.com") • Request = NSURLRequest (URL: url) webView.loadRequest (request)} Override funk done rescue memorivation ( } {Super.didReceiveMemoryWarning () / any resources that can be reproduced. }
Here's a very basic example: write it in textField:
Import UIKit Import WebKit Class ViewController: UIViewController, UITextFieldDelegate {@IBOutlet WEB webview: UIWebView! @Iboutlet weak var textfield: UITextField! @IBAction func Go (Sender: AnyObject) {var text = textField.text var url = NSURL (string: text) var req = NSURLRequest (URL: url!) Self.webView! .loadRequest (req)} override function viewDidLoad () {Super.viewDidLoad () self.textField.delegate = self}}
UISearchBar example shown
Import UIKit Import WebKit Class ViewController: UIViewController, UISearchBarDelegate {@IBOutlet Vulnerable var searchBar: UISearchBar! @ IBOutlet Weak Web Webview: UIWebView! Func searchBarSearchButtonClicked (searchBar: UISearchBar!) {SearchBar.resignFirstResponder () var text = searchBar.text var url = NSURL (string: text) // "http://www.apple.com" var req = NSURLRequest (URL : Url!) Self.webView! .loadRequest (req)} Override function viewDidload () {super.viewDidLoad (self.searchBar.delegate = self}}
Comments
Post a Comment