我有一个Input(UITextfied),我希望其中的数据DesA
在我的其他ViewController中更改
我的第一个ViewController:Facial(标签在其中DesA
)我的第二个ViewController:ServiceAChangeViewController(其中Input和button在其中)(很抱歉,名称很奇怪)
本质上,我想在自己的文本中写一个文本UITextField
,然后单击一个使用核心数据保存文本的按钮,然后DesA
在First View Controller中更改标签
我遵循了youtube上的一些教程并编写了此代码
第二个ViewController(ServiceAChangeViewController):变量得分= Facial(nibName:nil,包:nil).TitleChange var OutputLabel = Facial(nibName:nil,包:nil).DesA
``` @IBAction func saveData(_ sender: Any) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Entity", in: context)
let newEntity = NSManagedObject(entity: entity!, insertInto: context)
newEntity.setValue(score, forKey: "titleA")
do {
try context.save()
print("saved")
} catch {
print("Failed savign")
}
}
func getData() {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Entity")
request.returnsObjectsAsFaults = false
do {
if let result = try? context.fetch(request) as? [NSManagedObject] {
for data in result {
score = data.value(forKey: "titleA") as! String
}
} else {
print("Failed")
}
}
}
@IBAction func SubmitAnswer(_ sender: Any) {
OutputLabel?.text = score
score = String(InputA.text!)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
getData()
OutputLabel?.text = score
}```
第一个ViewController(面部):
@IBOutlet var TitleA: UILabel!
var TitleChange = String()
override func viewDidAppear(_ animated: Bool) {
}
但是,当我运行该应用程序并输入文本时,它似乎既没有更改也没有保存。
尝试使用此方法保存核心数据实体,也可以将“ didset”用于变量
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let newInfo = Entity(entity: Entity.entity(), insertInto: context)
// do something with entity
do {
try context.save() } catch let error as NSError {
print("Error... \(error), \(error)")
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句