Ios 重新显示视图控制器时出错

Ios 重新显示视图控制器时出错,ios,uitableview,core-data,Ios,Uitableview,Core Data,我提出了一个模态视图控制器,用户可以在其中输入一些信息,然后在使用此功能保存这些信息后 func handleSave() { guard let newProductUrl = NSURL(string: urlTextField.text!) else { print("error getting text from product url field") return } guard let newProductName = sel

我提出了一个模态视图控制器,用户可以在其中输入一些信息,然后在使用此功能保存这些信息后

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
视图中将出现一个错误(显示模态视图控制器的控制器,现在正在尝试返回)

编辑:将代码放入调度块中:

    DispatchQueue.main.async {
        self.productController?.save(name: newProductName, url: newProductUrl, image: newProductImage)

        let pController = ProductController()
        self.productController = pController
        self.navigationController?.pushViewController(pController, animated: true)
    }

这是您的问题,您使用了present(ac,animated:true,completion:nil)来访问此视图,因此此演示是模态的,并且没有navigationController。 您必须使用UINavigationController.pushViewController来显示视图,这样您将获得navigationController

编辑:

只是别忘了在storyboard中设置addProductController的标识符,将storyboard和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)

问题在于此调用中的块:

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)

但是,您可能想清理创建和取消视图控制器的方式/位置--这里的视图控制器看起来会不必要地堆积一堆视图控制器。

我已将其更改为
ac.pushViewController(pController,动画:true)
我现在遇到了一个错误,导致崩溃:“不支持多次推送同一个view controller实例”您不应该只更改代码中的一项内容,如果不起作用,请将ac更改为UINavigationController,请告诉我编辑我的帖子并输入完整的代码我不确定我是否理解你的要求-
UINavigationController.pushViewController(pController,动画:true)
不起作用。只需将我的帖子编辑到你的代码中,我对其进行了测试,这对我有效,并在导航栏中显示了第二个VC,这就是你需要的吗?我会尝试一下,但我不使用故事板,这都是程序性的,所以我不知道如何设置标识是的,这就是我遇到的问题。。我尝试将代码放入分派块中,但仍然得到了问题中提到的相同错误。我编辑了我的问题,向新代码展示了我的尝试(仍然习惯于在视图和核心数据中导航,因此请告诉我是否有更好的方法),如果不查看所有代码,很难判断您正在尝试做什么,但请尝试重新定义问题,以便只使用ProductController的一个实例。看起来你不需要每次保存某个内容时都创建一个新的。我知道,很抱歉,这对我来说是一个很大的学习项目,我在视图控制器/导航控制器中迷失了方向。我想我只有一个ProductController实例,然后是模态视图
AddProductController
。我真的很困惑,无法正确解释。。。如果您想查看错误(点击编辑->完成->点击公司并尝试添加产品以查看错误),这就是项目:您是否尝试过使用故事板实现此功能?这样的应用程序通常就是这样组合起来的。使用故事板可能会帮助你以一种可以避免错误的方式组织事情。我想在没有故事板的情况下完成它,如果我想使用故事板,我真的必须重新开始整个项目,我已经在这方面工作了一个多月了,实际上正是这个特殊的问题阻碍了我完成它。
            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)
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)