2014-10-20 29 views
0

当我使用swift代码触摸UISearchBar外部时,如何解除键盘消失。它适用于我在谷歌搜索页面上时,但当我在网站内部,当我触摸uisearchbar时,然后当我敲击它时,键盘不会消失。当我使用swift代码在UISearchBar外部触摸时,如何解除键盘

这里我的代码到目前为止。

import UIKit 
import WebKit 

class ViewController: UIViewController, UISearchBarDelegate { 


@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() 

    let url = NSURL(string: "http://google.com") 
    let request = NSURLRequest(URL: url) 
    webView.loadRequest(request) 

    self.searchBar.delegate = self 

    } 

} 

回答

2

您可以使用触摸事件dissmiss键盘

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) { 
self.view.endEditing(true) 
} 
+0

感谢。我刚刚插入之前,你的回应。它现在有效。你知道我可以在哪里快速学习吗? – 2014-10-20 08:03:01

相关问题