我有 2 個視圖控制器:
VC1 根據 CoreData 屬性 isPicked 填充其 tableView,該屬性是 bool 并且僅顯示具有true狀態的專案。VC2 是第二個模態(非全屏)視圖控制器,它允許用戶更改 isPicked 屬性的狀態:選中和取消選中專案(使其為真或假),按下保存按鈕后,更改將保存在 CoreData 中,VC2 將被解雇。但是 VC1 不會使用來自 VC2 的最新更改來更新自身。
我嘗試了什么:
- 加載我的陣列以在 viewWillAppear() 中的 VC1 中顯示,但由于我的 VC2 是模式視圖而不是全屏模式,因此此方法僅呼叫一次
- 試圖實作協議和委托,但結果是一樣的 - 無法重新加載資料(但假設我做錯了)
我的VC1:
override func viewDidLoad() {
super.viewDidLoad()
let predicate = NSPredicate(format: "isPicked == YES")
converterArray = load(for: tableView, and: predicate)
}
func load(for tableView: UITableView, with request: NSFetchRequest<Currency> = Currency.fetchRequest(), and predicate: NSPredicate? = nil, sortDescriptor: [NSSortDescriptor] = [NSSortDescriptor(key: "shortName", ascending: true)]) -> [Currency] {
var array = [Currency]()
request.predicate = predicate
request.sortDescriptors = sortDescriptor
do {
array = try context.fetch(request)
} catch {
print(error)
}
DispatchQueue.main.async {
tableView.reloadData()
}
return array
}
我的VC2:
@IBAction func saveButtonPressed(_ sender: UIBarButtonItem) {
do {
try context.save()
} catch {
print(error)
}
dismiss(animated: true)
}
另外請看一個簡短的 GIF,我在其中展示了發生的情況:點擊
我不知道 VC2 解散后如何自動更新 VC1 中的更改...
uj5u.com熱心網友回復:
我假設資料已正確保存在 VC2 中,您遇到的唯一問題是在 VC1 中重新加載資料的正確方法。
使 VC1 符合UIViewControllerTransitioningDelegate并實作其委托功能animationController(forDismissed dismissed: UIViewController)可能會解決您的問題
在這里閱讀更多
試試這個
// Some function in VC1 which loads VC2
func loadVC2()
{
let newVC = UIViewController()
// This is important
newVC.transitioningDelegate = self
present(newVC, animated: true, completion: nil)
}
然后在VC1中,實作函式animationController(forDismissed dismissed: UIViewController)
extension VC1: UIViewControllerTransitioningDelegate
{
// UIViewControllerTransitioningDelegate delegate function auto invoked during modal transitions
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
{
print("returned back")
// reload your table in VC1 here
/// Returning nil as per guidelines
return nil
}
}
試一試,看看這是否能解決您的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/422255.html
標籤:
