如何在 MasterViewController 更新时刷新 DetailViewController
How to refresh DetailViewController when MasterViewController is updated
我有一个应用程序,用户可以在其中更新和添加任务。但是当用户更新任务时,我需要更新 DetailViewController
中的详细信息
当用户更新任务并保存MasterViewController中的数据时,DetailViewController中的数据保持不变
我怎样才能修复它以便更新详细信息
使用 AddProjectController
添加和编辑任务
我尝试了以下方法,但none如果它们有效,都会导致应用程序崩溃
let vc = DetailViewController()
let row = vc.view.layoutIfNeeded();
//Second method
let vc = MasterViewController()
vc.performSegue(withIdentifier: "showDetail", sender: vc.self)
使用
在DetailViewController中设置数据
override func viewDidLoad() {
super.viewDidLoad()
table.delegate = self
table.dataSource = self
lblName.text = detail.name ?? ""
lblCode.text = detail.code ?? ""
}
MasterViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let object = fetchedResultsController.object(at: indexPath)
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
controller.details = object;
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
detailViewController = controller
}
}
}
编辑视图控制器
func editEvents(_ sender: Any){
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Proj")
do {
let test = try context.fetch(fetchRequest)
var objectUpdate = test[0] as! Assesment;
objectUpdate.name = txtname.text ?? ""
objectUpdate.code = txtCode.text ?? "";
do {
try context.save()
}
catch {
print(error)
}
}
catch {
print(error)
}
}
我想在 editEvent 函数执行完毕后更新详情
有什么方法可以让它工作吗?
如果你正在使用 coreData ..你可以详细实现 FRC ..这样你每次在回调中都能得到刷新的数据......
在你的MasterViewController
中定义一个变量
这是另一个解决方法
private var controller : DetailViewController? = nil
然后在你的 Segue 方法中使用这个控制器
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let object = fetchedResultsController.object(at: indexPath)
self.controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController // here you set your instence variable controller
controller.details = object;
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
detailViewController = controller
}
}
}
func editEvents(_ sender: Any){
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Proj")
do {
let test = try context.fetch(fetchRequest)
var objectUpdate = test[0] as! Assesment;
objectUpdate.name = txtname.text ?? ""
objectUpdate.code = txtCode.text ?? "";
do {
try context.save()
if let detailsCotrollerExists = controller {
detailsCotrollerExists.updateDataWithObject(objectUpdate) // function you will write in detail class which takes object and refresh data there .. Either outlets or Table/Collection View reload
}
}
catch {
print(error)
}
}
catch {
print(error)
}
}
然后你在 DetailViewController
中写一个函数 func updateDataWithObject(_ obj: Assesment)
你将详细写的函数 class 它获取对象并在那里刷新数据.. 出口或 Table/Collection 查看重新加载
在DetailViewController
中写入函数
func updateDataWithObject(_ obj: Assesment) {
// Refresh data
}
我有一个应用程序,用户可以在其中更新和添加任务。但是当用户更新任务时,我需要更新 DetailViewController
当用户更新任务并保存MasterViewController中的数据时,DetailViewController中的数据保持不变
我怎样才能修复它以便更新详细信息
使用 AddProjectController
添加和编辑任务我尝试了以下方法,但none如果它们有效,都会导致应用程序崩溃
let vc = DetailViewController()
let row = vc.view.layoutIfNeeded();
//Second method
let vc = MasterViewController()
vc.performSegue(withIdentifier: "showDetail", sender: vc.self)
使用
在DetailViewController中设置数据 override func viewDidLoad() {
super.viewDidLoad()
table.delegate = self
table.dataSource = self
lblName.text = detail.name ?? ""
lblCode.text = detail.code ?? ""
}
MasterViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let object = fetchedResultsController.object(at: indexPath)
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
controller.details = object;
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
detailViewController = controller
}
}
}
编辑视图控制器
func editEvents(_ sender: Any){
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Proj")
do {
let test = try context.fetch(fetchRequest)
var objectUpdate = test[0] as! Assesment;
objectUpdate.name = txtname.text ?? ""
objectUpdate.code = txtCode.text ?? "";
do {
try context.save()
}
catch {
print(error)
}
}
catch {
print(error)
}
}
我想在 editEvent 函数执行完毕后更新详情
有什么方法可以让它工作吗?
如果你正在使用 coreData ..你可以详细实现 FRC ..这样你每次在回调中都能得到刷新的数据......
在你的MasterViewController
中定义一个变量
这是另一个解决方法
private var controller : DetailViewController? = nil
然后在你的 Segue 方法中使用这个控制器
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let object = fetchedResultsController.object(at: indexPath)
self.controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController // here you set your instence variable controller
controller.details = object;
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
detailViewController = controller
}
}
}
func editEvents(_ sender: Any){
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Proj")
do {
let test = try context.fetch(fetchRequest)
var objectUpdate = test[0] as! Assesment;
objectUpdate.name = txtname.text ?? ""
objectUpdate.code = txtCode.text ?? "";
do {
try context.save()
if let detailsCotrollerExists = controller {
detailsCotrollerExists.updateDataWithObject(objectUpdate) // function you will write in detail class which takes object and refresh data there .. Either outlets or Table/Collection View reload
}
}
catch {
print(error)
}
}
catch {
print(error)
}
}
然后你在 DetailViewController
中写一个函数 func updateDataWithObject(_ obj: Assesment)
你将详细写的函数 class 它获取对象并在那里刷新数据.. 出口或 Table/Collection 查看重新加载
在DetailViewController
func updateDataWithObject(_ obj: Assesment) {
// Refresh data
}