gpt4 book ai didi

ios - 将核心数据值传递到文本字段中以进行编辑/保存

转载 作者:行者123 更新时间:2023-11-30 12:05:14 27 4
gpt4 key购买 nike

我是使用 Xcode 和 swift 的新手,仍在学习中。我在将核心数据从实体传递到文本字段/标签时遇到问题,然后用户可以选择编辑和保存记录。我的目标是,当用户从 friendslistViewController.swift 单击 tableView 中的单元格时,它会将他们带到 editViewController.swift 屏幕以编辑和保存记录。我想我需要实现 prepareforsegue 函数,但不确定如何实现?

friendsViewController.swift(初始数据输入)

//
// friendsViewController.swift
//
//
// Created by on 11/10/17.
// Copyright © 2017. All rights reserved.
//

import UIKit
import CoreData

class friendsViewController: UIViewController {

//OUTLETS


@IBOutlet weak var firstName: UITextField!
@IBOutlet weak var lastName: UITextField!
@IBOutlet weak var genderSegControl: UISegmentedControl!
@IBOutlet weak var ageStepper: UIStepper!

//AGECOUNT
@IBOutlet weak var ageLabel: UILabel!
@IBOutlet weak var address: UITextField!

var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}

//actions

@IBAction func ageStepperStep(_ sender: UIStepper) {
let step = Int(sender.value) // using an integer variable
ageLabel.text = String(step)
// label1.text = Int(sender.value).description
// another way without using any integer variable
}


// TEST GENDER
/*

var gender : String
if self.genderSegControl.selectedSegmentIndex == 0 {
gender = "male"
}
else {
gender = "female"
}
genderLabel.text! = gender

*/

//SAVE FRIEND BUTTON
@IBAction func saveFriendBtn(_ sender: Any) {

if firstName.text != "" && lastName.text != "" {

var gender : String?
if self.genderSegControl.selectedSegmentIndex == 0 {
gender = "male"
}
else {
gender = "female"
}


let newFriend = NSEntityDescription.insertNewObject(forEntityName: "Friends", into: context)
newFriend.setValue(self.firstName.text, forKey: "firstName")
newFriend.setValue(self.lastName.text, forKey: "lastName")
newFriend.setValue(self.address.text, forKey: "address")
newFriend.setValue(self.ageLabel.text, forKey: "age")
newFriend.setValue(gender, forKey: "gender")

/*let gender: String = genderSegControl.titleForSegment(at: genderSegControl.selectedSegmentIndex)!
*/


//SAVE THE CONTEXT
do {
try context.save()
}
catch{
print(error)
}
}
else {
print("please enter your first and last name!")
}
}

} //end class

friendslistViewController.swift(显示带有单元格的 tableView 的屏幕)

//
// friendslistViewController.swift
//
//
// Created by on 11/10/17.
// Copyright © 2017. All rights reserved.
//

import UIKit
import CoreData

class friendslistViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

/*
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var DestViewController : editViewController = segue.destinationViewController as! editViewController

DestViewController.firstNameInput = name.firstName!
}
*/

//outlets

@IBOutlet weak var tableView: UITableView!

var userArray:[Friends] = []

override func viewDidLoad() {
super.viewDidLoad()

tableView.delegate = self
tableView.dataSource = self

self.fetchData()
self.tableView.reloadData()


// Do any additional setup after loading the view.
}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userArray.count
}


//DISPLAY LISTVIEW
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
//save first users data into array
let name = userArray[indexPath.row]
cell.textLabel!.text = name.firstName! + " " +
name.lastName! + ", " +
name.age! + ", " +
name.gender! + ", " +
name.address!

return cell
}


func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

if editingStyle == .delete{

//delete a friend
let friend = userArray[indexPath.row]
context.delete(friend)
(UIApplication.shared.delegate as! AppDelegate).saveContext()

do{
userArray = try context.fetch(Friends.fetchRequest())

}catch{
print(error)
}

}
tableView.reloadData()

}

//fetch the data
func fetchData(){
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

do{
userArray = try context.fetch(Friends.fetchRequest())
}
//if error exists/catch it
catch{
print(error)
}
}

//actions
} //end class

editViewController.swift(处理编辑/保存的屏幕)

//
// editViewController.swift
//
//
// Created by on 15/10/17.
// Copyright © 2017. All rights reserved.
//

import UIKit

import CoreData

class editViewController: UIViewController {

@IBOutlet weak var firstName: UITextField!
@IBOutlet weak var lastName: UITextField!
@IBOutlet weak var ageLabel: UILabel!
@IBOutlet weak var address: UITextField!

var firstNameInput = String()

override func viewDidLoad() {
super.viewDidLoad()

firstName.text = firstNameInput

// Do any additional setup after loading the view.
}

}

最佳答案

首先,您应该更改 View Controller 的名称,使它们全部以大写字母开头(例如 class FriendsViewController: UIViewController),并对文件名执行相同的操作。

其次,您关于需要添加转场的说法是正确的。您查看过 Apple 文档中的 func prepare(for segue: UIStoryboardSegue, sender: Any?) 吗?

您在目标 View Controller (editViewController.swift) 中需要一个变量,您可以将 Friend 对象分配给该变量。在您的 prepare(for:sender:) 方法中,您需要类似 let destinationViewController = segue.destination as!编辑 View Controller 。然后,您可以使用该 destinationViewController 变量来访问目标 View Controller 中的变量并将对象分配给它。

看看你能走多远。

关于ios - 将核心数据值传递到文本字段中以进行编辑/保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46751677/

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