我提出了一个模态视图控制器,用户可以在其中输入一些信息,然后使用此功能保存该信息...
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)
}
...我在viewWillAppear
ProductController
(显示模态视图控制器的控制器,现在正试图回复)中收到错误
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)")
}
}
错误是:在let companyToDisplay = self.navigationItem.title!
行上打开一个可选项时意外发现nil。如何指定它所寻找(和缺失)的self.navigationItem.title
是发送模态视图的控制器的self.navigationItem.title
?
感谢您提供任何帮助,我一直试图将此问题排除数日并且无法解决问题。
编辑:这就是我在AddProductController
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)
}
编辑:将代码放入调度块:
DispatchQueue.main.async {
self.productController?.save(name: newProductName, url: newProductUrl, image: newProductImage)
let pController = ProductController()
self.productController = pController
self.navigationController?.pushViewController(pController, animated: true)
}
答案 0 :(得分:0)
这是你的问题,你使用present(ac,animated:true,completion:nil)来获取这个视图,所以这个演示文稿是模态的,没有navigationController。 您必须使用UINavigationController.pushViewController来呈现视图,这样您将获得navigationController。
编辑:
let nc = UINavigationController()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let addProductController = storyboard.instantiateViewController(withIdentifier: "addProductVC")
nc.viewControllers = [addProductController]
self.modalTransitionStyle = UIModalTransitionStyle.coverVertical
self.modalPresentationStyle = .currentContext
self.present(nc, animated: true, completion: nil)
不要忘记在故事板中设置addProductController的标识符,将故事板和VC标识符更改为您的标识符。
编辑2:
let nc = UINavigationController()
let addProductController = ProductController()
addProductController.navigationItem.title = "Have a good one"
nc.viewControllers = [addProductController]
self.modalTransitionStyle = UIModalTransitionStyle.coverVertical
self.modalPresentationStyle = .currentContext
self.present(nc, animated: true, completion: nil)
答案 1 :(得分:0)
问题在于此调用中的阻止:
DispatchQueue.main.async {
self.productController?.save(name: newProductName, url: newProductUrl as URL, image: newProductImage)
}
实际执行后,handleSave()
方法返回。看起来你期望它相对于该方法中的其他代码顺序执行。
DispathQueue.main.async
将一个块添加到将来某个时间点执行的代码队列中 - 它不会立即执行。
要解决此问题,您需要将代码放在调度块中,以执行下一步需要执行的操作。这与你在这里的相似:
// 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)
但是你可能想要清理你创建和解雇视图控制器的方式/位置 - 你在这里看到它会不必要地堆积一堆视图控制器。