我提出一個模式視圖控制器中,用戶可以輸入一些信息,然後在保存有此功能的信息......錯誤時重新呈現視圖控制器
func handleSave() {
guard let newProductUrl = NSURL(string: urlTextField.text!) else {
print("error getting text from product url field")
return
}
guard let newProductName = self.nameTextField.text else {
print("error getting text from product name field")
return
}
guard let newProductImage = self.logoTextField.text else {
print("error getting text from product logo field")
return
}
DispatchQueue.main.async {
self.productController?.save(name: newProductName, url: newProductUrl as URL, image: newProductImage)
}
// Present reloaded view controller with new product added
let ac = UINavigationController()
let pController = ProductController()
productController = pController
ac.viewControllers = [pController]
present(ac, animated: true, completion: nil)
}
...我得到一個錯誤在ProductController
的viewWillAppear
(該控制器提供的模態視圖控制器,現在正試圖要回)
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext =
appDelegate.persistentContainer.viewContext
let companyToDisplay = self.navigationItem.title!
let fetchRequest =
NSFetchRequest<NSManagedObject>(entityName: "Product")
fetchRequest.predicate = NSPredicate(format:"company.name == %@",companyToDisplay)
do {
products = try managedContext.fetch(fetchRequest)
print(products)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}
的錯誤是:意外地發現零,同時展開一個可選的,就行了。我如何指定它正在查找(和丟失)的self.navigationItem.title
是發送模態視圖的控制器的self.navigationItem.title
?
感謝您的任何幫助,我一直在試圖將這個問題排除幾天,無法弄清楚。
編輯:這是我從我的ProductController
func presentModalView() {
let nc = UINavigationController()
let addProductController = AddProductController()
nc.viewControllers = [addProductController]
self.modalTransitionStyle = UIModalTransitionStyle.coverVertical
self.modalPresentationStyle = .currentContext
self.present(nc, animated: true, completion: nil)
}
EDIT呈現模式的看法AddProductController
:把代碼調度塊中:
DispatchQueue.main.async {
self.productController?.save(name: newProductName, url: newProductUrl, image: newProductImage)
let pController = ProductController()
self.productController = pController
self.navigationController?.pushViewController(pController, animated: true)
}
我已將它更改爲'ac。pushViewController(P控制器,動畫:真)',我現在得到一個崩潰,出現錯誤:「推不止一次同樣的看法控制器實例不支持」 – d0xi45
你不應該只是改變你的代碼一兩件事,改變交流到UINavigationController的,如果它沒有工作,請告訴我編輯自己的帖子,並把完整的代碼 –
我不知道我理解你的要求 - 'UINavigationController.pushViewController(P控制器,動畫:真)'不起作用。 – d0xi45