在我的代码中:
@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
。有没有人有办法解决吗?
答案 0 :(得分:4)
在您的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, 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
上的Keyboard
将关闭键盘,则必须安装此TextFieldDelegate
方法
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.textField.resignFirstResponder()
return true
}
答案 1 :(得分:3)
使用此委托方法
class ViewController: UIViewController,UITextFieldDelegate //set delegate to class
@IBOutlet var txtValue: UITextField //create a textfile variable
override func viewDidLoad() {
super.viewDidLoad()
txtValue.delegate = self //set delegate to textfile
}
func textFieldDidBeginEditing(textField: UITextField!) { //delegate method
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
}
func textFieldShouldEndEditing(textField: UITextField!) -> Bool { //delegate method
return false
}
func textFieldShouldReturn(textField: UITextField!) -> Bool { //delegate method
textField.resignFirstResponder()
return true
}