【发布时间】:2015-03-20 03:52:03
【问题描述】:
当用户触摸它的外部时关闭这个键盘的好方法是什么?我尝试了接触开始的方法,但这不起作用。 textfieldshouldreturn 方法虽然可以正常工作。我想不出任何其他方法来做到这一点。有人可以在这里帮助我吗,我将非常感谢任何帮助。谢谢
import UIKit
var courseCatalog : [String] = Array<String>()
class HomeViewController: UIViewController, UITextFieldDelegate , UITableViewDelegate{
@IBOutlet var searchTextField: UITextField!
override func viewDidLoad() {
courseCatalog = ["Accounting", "Administration" , "American Studies" , "Anthropology" , "Arabic" , "Art" , "Aerospace Studies" , "American Sign Language" , "Biology" , "Child Development" , "Chemistry" , "Chinese" , "Criminal Justice", "Communication Studies" , "Computer Science", "Computer Engineering"]
super.viewDidLoad()
println(PFUser.currentUser())
println("Home Tab View Controller")
searchTextField.delegate = self
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return courseCatalog.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel?.text = courseCatalog[indexPath.row]
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.searchTextField.resignFirstResponder()
}
func textFieldShouldReturn(textField: UITextField!) -> Bool {
searchTextField.resignFirstResponder()
return true
}
}
【问题讨论】: