在我的代码中:
@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.有没有人有办法解决吗?
解决方法
在ViewController中,导入协议UITextFieldDelegate,如下所示:
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,withRowAnimation: .Top) func textFieldShouldReturn(textField: UITextField) -> Bool { self.textField.resignFirstResponder() return true }}) }
如果要单击键盘上的Return键将关闭键盘,则必须安装此TextFieldDelegate方法
func textFieldShouldReturn(textField: UITextField) -> Bool { self.textField.resignFirstResponder() return true }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。