在我的代码中: @IBOutlet weak var textField: UITextField!@IBAction func textField(sender: AnyObject) { if textField.text != "" { let newSweet = CKRecord(recordType: "Chat") newSweet["content"] = textField.text textField.delegate = se
@IBOutlet weak var textField: UITextField! @IBAction func textField(sender: AnyObject) { if textField.text != "" { let newSweet = CKRecord(recordType: "Chat") newSweet["content"] = textField.text textField.delegate = self let publicData = CKContainer.defaultContainer().publicCloudDatabase publicData.saveRecord(newSweet, completionHandler: { (record:CKRecord?, error:NSError?) -> Void in if error == nil { dispatch_async(dispatch_get_main_queue(), { () -> Void in self.tableView.beginUpdates() self.sweets.insert(newSweet, atIndex: 0) let indexPath = NSIndexPath(forRow: 0, inSection: 0) self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Top) func textFieldShouldReturn(textField: UITextField) -> Bool { self.textField.resignFirstResponder() return true }}) }else{ print(error) } }) } }
我正在尝试访问cloudKit,因此当您在UITextField中键入内容时,它会激活UITextField @IBAction.
现在当我在UITextField中输入文本时没有任何反应,所以我需要它来激活@IBAction.有没有人有办法解决吗?
class YourViewController: UIViewController,UITextFieldDelegate
在你的viewDidLoad中这样做:
override func viewDidLoad() { super.viewDidLoad() textField.delegate = self }
你必须安装委托方法:
func textFieldDidBeginEditing(textField: UITextField!) { let newSweet = CKRecord(recordType: "Chat") newSweet["content"] = textField.text textField.delegate = self let publicData = CKContainer.defaultContainer().publicCloudDatabase publicData.saveRecord(newSweet, completionHandler: { (record:CKRecord?, error:NSError?) -> Void in if error == nil { dispatch_async(dispatch_get_main_queue(), { () -> Void in self.tableView.beginUpdates() self.sweets.insert(newSweet, atIndex: 0) let indexPath = NSIndexPath(forRow: 0, inSection: 0) self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Top) func textFieldShouldReturn(textField: UITextField) -> Bool { self.textField.resignFirstResponder() return true }}) }
如果要单击键盘上的Return键将关闭键盘,则必须安装此TextFieldDelegate方法
func textFieldShouldReturn(textField: UITextField) -> Bool { self.textField.resignFirstResponder() return true }