我有一个旋转木马,每个旋转木马项目都包含一个独特的桌面视图。当您点击添加按钮时,它会转到新的视图控制器,以便您可以为tableview提供名称并关联要在每个单元格中显示的项目。现在当我点击保存按钮时,它会执行segue回到轮播的viewcontroller。我的prepareforsegue
看起来像这样:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! BillSplittersViewController
let passedBill: NSManagedObject = bill as NSManagedObject
destinationVC.allBillSplitters = getUpdatedSplitters()
print(2)
print(destinationVC.allBillSplitters)
destinationVC.billName = billName
destinationVC.bill = passedBill
}
当显示轮播视图控制器时,它不显示从allBillSplitters
阵列加载的轮播中添加的项目。如果我尝试重新加载viewdidload
中的轮播,viewwillappear
或view willlayoutsubviews
没有任何更改。如果我在viewdidlayoutsubviews
中进行更新,它会更新,但您可以看到这种情况并不好。
要进行调试,我尝试在prepareforsegue
中打印数组,然后在viewdidload
中打印数组,并在prepareforsegue
中打印数组而不添加项目,并在viewdidload
中执行相同的操作 - 但随后它会使用添加的项目再次打印它们 - 但我无法在加载的视图中看到它。
如果有帮助,我正在使用iCarousel。我是新手,不确定发生了什么,所以我不知道使用什么代码。我正在为两个视图控制器使用故事板,并且segue附加到视图而不是按钮本身。任何帮助都会很棒。谢谢!
更多信息:
@IBAction func saveButtonWasPressed() {
let managedContext = bill.managedObjectContext
if let splitter = splitter {
splitter.mutableSetValue(forKey: "items").removeAllObjects()
setBillSplitterValues(splitter)
setSelectedItemsToBillSplitter(splitter)
} else {
let entity = NSEntityDescription.entity(forEntityName: "BillSplitter", in: managedContext!)
let newBillSplitter = NSManagedObject(entity: entity!, insertInto: managedContext)
setBillSplitterValues(newBillSplitter)
setSelectedItemsToBillSplitter(newBillSplitter)
}
do {
try managedContext!.save()
}
catch let error as NSError {
print("Core Data save failed: \(error)")
}
self.performSegue(withIdentifier: "segueToBillSplitters", sender: self)
}
检索新的billSplitters:
func getUpdatedSplitters() -> [BillSplitter] {
var allBillSplitters = [BillSplitter]()
let managedContext = bill.managedObjectContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "BillSplitter")
let predicate = NSPredicate(format: "ANY bills == %@", bill)
fetchRequest.predicate = predicate
do {
let results =
try managedContext!.fetch(fetchRequest)
allBillSplitters = results as! [BillSplitter]
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
return allBillSplitters
}
因此,当按下保存时,它会将现有的“billSplitter”更新为coredata或添加新的,然后调用performSegue
。在prepareForSegue
中,然后从CoreData中获取所有'billSplitters'并将它们传递给目标视图控制器。
我认为NSManagedObjectContextDidSave
观察员如果在收到通知后调用performSegue
但没有任何更改,可能会有所帮助。
更新
我在saveButtonWasPressed
方法中尝试过以下操作。当viewcontroller加载时,它会快速显示旧的轮播然后闪烁并显示更新的轮播。我不知道从哪里开始。
@IBAction func saveButtonWasPressed() {
DispatchQueue.global(qos: .background).async { [weak weakSelf = self] in
let managedContext = weakSelf?.bill.managedObjectContext
if let splitter = weakSelf?.splitter {
splitter.mutableSetValue(forKey: "items").removeAllObjects()
weakSelf?.setBillSplitterValues(splitter)
weakSelf?.setSelectedItemsToBillSplitter(splitter)
} else {
let entity = NSEntityDescription.entity(forEntityName: "BillSplitter", in: managedContext!)
let newBillSplitter = NSManagedObject(entity: entity!, insertInto: managedContext)
weakSelf?.setBillSplitterValues(newBillSplitter)
weakSelf?.setSelectedItemsToBillSplitter(newBillSplitter)
}
do {
try managedContext!.save()
}
catch let error as NSError {
print("Core Data save failed: \(error)")
}
DispatchQueue.main.async { [weak weakSelf = self] in
guard let weakSelf = weakSelf else { return }
weakSelf.performSegue(withIdentifier: "segueToBillSplitters", sender: self)
}
}
}
答案 0 :(得分:0)
我可能不明白你究竟在问什么,但我唯一的输入是,当我使用准备segue时,我总是使用segue标识符
var valueToPass
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if (segue.identifier == "yourSegue") {
let viewController = segue.destination as! ViewController
viewController.passedValue = valueToPass as String
}
您在故事板中声明您的标识符,同时确保您的segue是一个动作segue