2014-10-20 102 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

相關問題