如何解决如何使用新名称
我想从AddViewController更新itemViewController中的行名。在我的代码中,我将包含名称和说明的item对象传递给了我的AddViewController并更改了值,但是行名称从未更新。
我环顾了互联网,发现了有关所谓的委托模式的信息。我只是不确定如何实现该模式。谢谢您的提前帮助。
// ItemsViewController.swift
// Project 3-Todo
//
// Created by Gracias Claude on 10/9/20.
//
import UIKit
class ItemsViewController : UITableViewController {
var itemStore: ItemStore!
@IBAction func addNewItem(_ sender: UIButton) {
// Create a new item and add it to the store
let newItem = itemStore.createItem()
// figure out where that item is in the array
if let index = itemStore.allItems.firstIndex(of: newItem) {
let indexPath = IndexPath(row: index,section: 0)
// Insert this new row into the table
tableView.insertRows(at: [indexPath],with: .automatic)
}
}
@IBAction func toggleEditingMode(_ sender: UIButton) {
// If you are currently in editing mode...
if isEditing {
// Change text of button to inform user of state
sender.setTitle("Edit",for: .normal)
// Turn off editing mode
setEditing(false,animated: true)
} else {
// Change text of button to inform user of state
sender.setTitle("Done",for: .normal)
// Enter editing mode
setEditing(true,animated: true)
}
}
override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return itemStore.allItems.count
}
// returning table view cell
override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Get a new or recycled cell
let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell",for: indexPath)
// Set the text on the cell with the description of the item
// that is at the nth index of items,where n = row this cell
// will appear in on the table view
let item = itemStore.allItems[indexPath.row]
cell.textLabel?.text = item.name
cell.detailTextLabel?.text = "\(item.dateCreated)"
return cell
}
override func tableView(_ tableView: UITableView,commit editingStyle: UITableViewCell.EditingStyle,forRowAt indexPath: IndexPath) {
// If the table view is asking to commit a delete command...
if editingStyle == .delete {
let item = itemStore.allItems[indexPath.row]
// Remove the item from the store
itemStore.removeItem(item)
// Also remove that row from the table view with an animation
tableView.deleteRows(at: [indexPath],with: .automatic)
}
}
override func tableView(_ tableView: UITableView,moveRowAt sourceIndexPath: IndexPath,to destinationIndexPath: IndexPath) {
// Update the model
itemStore.moveItem(from: sourceIndexPath.row,to: destinationIndexPath.row)
}
override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
// If the triggered segue is the "showItem" segue
switch segue.identifier {
case "showItem":
print("Hello World")
// figure out which row was just tapped
if let row = tableView.indexPathForSelectedRow?.row {
// Get the item associated with this row and pass it along
let item = itemStore.allItems[row]
let detailViewController
= segue.destination as! DetailsViewController
detailViewController.item = item
}
break;
case "addItem":
let row = tableView.indexPathForSelectedRow?.row ?? 0
// // Get the item associated with this row and pass it along
let item = itemStore.allItems[row]
let addViewController
= segue.destination as! AddViewController
addViewController.item = item
// }
break;
default:
preconditionFailure("Unexpected segue identifier.")
}
}
}
//AddViewController
//
// AddViewController.swift
// Project 3-Todo
//
// Created by Gracias Claude on 10/9/20.
//
import UIKit
class AddViewController : UIViewController {
var item: Item!;
@IBOutlet weak var itemNameField: UITextField!
@IBOutlet weak var priorityField: UITextField!
@IBOutlet weak var descriptionField: UITextField!
@IBAction func submitItem(_ sender: UIButton) {
//how to update the existing Row name
item.name = itemNameField.text ?? "Default Text"
item.description = descriptionField.text ?? "default Description"
return;
}
}
解决方法
据我了解,您正在将数据从ItemViewController
传递到AddViewController
。然后,您可以从AddViewController
更改该值,并希望该更改反映回ItemViewController
。
如果是这种情况,则必须将修改后的数据传回给ItemViewController
,并且有很多方法可以做到这一点。
最简单的方法(我认为这不是最佳实践)是在ItemViewController
内创建一个函数来捕获新更改。
func captureModified(itemStore: ItemStore) {
self.itemStore = itemStore
}
接下来,当您从ItemViewController
过渡到AddViewController
时,请在ItemViewController
中创建AddViewController
的实例,并在从ItemViewController
过渡到AddViewController
时对其进行初始化。 class AddViewController: UIViewController {
weak var itemViewController: ItemViewController?
}
。
ItemViewController
从AddViewController
到let addViewController = segue.destination as! AddViewController
addViewController.itemViewController = self
的过渡
deinit
最后,当您过渡回来时;您可以在AddViewController
方法中执行此操作,也可以在deinit {
itemViewController.captureModified(itemStore: itemStore)
}
方法中创建自己的back方法。
captureModified(_:)
这是做事的最简单方法。最安全和最佳的做法是将ItemViewController
方法包装在协议中,并创建该协议的实例,而不是整个print
类。让我知道您是否愿意这样做,我可以实施。或者在时间允许的情况下,不管将来的读者如何,我都会把它放进去。
我向String
的新segue
回到了AddViewController
itemViewController
在 switch segue.identifier {
case "added":
let itemsViewController = segue.destination as! ItemsViewController
itemsViewController.itemStore = itemStore
default:
preconditionFailure("Unexpected segue identifier.")
中保持单元格索引的轨迹
itemsViewController
并向 @IBAction func addNewItem(_ sender: UIButton) {
// Create a new item and add it to the store
let newItem = itemStore.createItem()
// Figure out where that item is in the array
if let index = itemStore.allItems.firstIndex(of: newItem) {
let indexPath = IndexPath(row: index,section: 0)
self.row = index
// Insert this new row into the table
tableView.insertRows(at: [indexPath],with: .automatic)
}
}
传递了要更新的正确行,以便AddViewController
知道正在更新的那一行
ItemViewController
我还在
let row = self.row
// // Get the item associated with this row and pass it along
let item = itemStore.allItems[row]
let itemStore = self.itemStore;
let addViewController
= segue.destination as! AddViewController
addViewController.item = item
addViewController.itemStore = itemStore
addViewController.row = self.row;
// }
break;
default:
preconditionFailure("Unexpected segue identifier.")
}
的顶部添加了它,以拥有AddViewController
itemViewController
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。