我在swift 3.0中做了一个项目,我有两个UIViewControllers,其中一个有三个文本字段。在UIViewController中,一旦在文本字段中输入数据,并且当按下保存按钮时,数据将被保存到核心数据中。在我的核心数据模块中,我的实体的名称是"任务"它有四个属性,即:金额,年龄,名称和重要(除最后一个之外都是字符串)。代码如下所示
import UIKit
class AddTaskViewController: UIViewController {
var isRowTapped :Bool?
@IBOutlet weak var textFiels: UITextField!
@IBOutlet weak var firstTextField: UITextField!
@IBOutlet weak var secondTextField: UITextField!
@IBOutlet weak var isImp: UISwitch!
override func viewDidLoad() {
}
@IBAction func buttonTabbed(_ sender: AnyObject) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let task = Task(context: context)
task.name = textFiels.text
task.age = firstTextField.text
task.amount = secondTextField.text
task.isImportant = isImp.isOn
(UIApplication.shared.delegate as! AppDelegate).saveContext()
print("Data saved successfully")
navigationController!.popViewController(animated: true)
}
}
即使将三个值保存到核心数据,也只有名为" name"将从核心数据中取出,并将打印在第二个视图控制器中的单元格上,在那里我有我的表格视图(它更像是一个身份名称)。我想知道的是,因为我在初始视图控制器中最初输入三个值到核心数据,一旦在第二个视图控制器中选择了特定行,我如何获得与该名称属性对应的所有三个值并将它们重新分配给初始视图控制器中的文本字段,以便我可以编辑它们并再次保存。我的第二个视图控制器的代码如下(tableviewViewController)。
import UIKit
class DispalyViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableview: UITableView!
var tasks : [Task] = []
var isRowSelected :Bool = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
loadData()
self.tableview.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadData(){
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do {
tasks = try context.fetch(Task.fetchRequest())
print("fetch sucess")
}catch{
print("fetch failed")
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("count is : \(tasks.count)")
return tasks.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell ()
print("table view loaded")
let task = tasks[indexPath.row]
if task.isImportant{
cell.textLabel?.text = " \(task.name!)"
}else{
cell.textLabel?.text = task.name
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "ShowNxtVC", sender: nil)
//isRowSelected = true
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let selectedRow = segue.destination as! AddTaskViewController
//selectedRow.isRowTapped = isRowSelected
}
//For swipe access allow
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
if editingStyle == .delete {
let task = tasks [indexPath.row]
context.delete(task)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
do {
tasks = try context.fetch(Task.fetchRequest())
}catch{
print("fetchinh failed")
}
}
tableview.reloadData()
}
}