gpt4 book ai didi

swift - 在哪里放置更新核心数据函数(swift)

转载 作者:行者123 更新时间:2023-11-30 11:30:06 25 4
gpt4 key购买 nike

我对如何更新核心数据做了一些研究,并找到了 setValue() 函数。

我现在正在努力寻找在哪里调用这个函数。显然我会在数据更改时调用它:

if (textField.text != detail!.valueForKey("name")!.description {
detail?.setValue(textField.text, forKey: "name")
}

我应该把它放在哪里?我怀疑它属于viewDidLoad和configureView。这是我的detailViewController 文件。

文本字段是:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TextFieldTableViewCell
let textField: UITextField = cell.textField
let detail = self.detailItem

textField.text = detail!.valueForKey("name")!.description
return cell
}
<小时/>

已解决

textFieldDidEndEditing()解决了检查值是否更改并保存的问题。

传递一个 UITableViewCell 子类的 textField 在这里解决:How to call textField that is a inside of UITableViewCell (swift)

最佳答案

class ViewController: UIViewController,UITextFieldDelegate {

@IBOutlet weak var txtName: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
txtName.delegate = self
}

func textFieldDidEndEditing(_ textField: UITextField) {
print("Tells the delegate that editing ends for specific textfield")

if (textField.text != detail!.valueForKey("name")!.description {
detail?.setValue(textField.text, forKey: "name")
}
}
}

希望这会有所帮助..

关于swift - 在哪里放置更新核心数据函数(swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50342901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com