ios - 如何多次推送同一个 ViewController?

标签 ios swift uiviewcontroller uinavigationcontroller nib

我有一个列表,我想显示它 3 次(当我单击特定单元格时,从一个列表到另一个列表)。为此,我正在使用包含我的 ListView 的 XIB。我还设置了一个 NavigationController 来播放推送动画。

这就是我在 rootViewController 中调用 pushViewController 方法的方式。

    case "Groupe" :
        //this is OK since it's the first time I push the ViewController
        service.getGroupe()
        let vueGroupe = vueListe!
        navigationController?.pushViewController(vueGroupe, animated: true)
        vueListe.navigationItem.title = "Groupe View";
    case "Categorie" :
        //this is not
        let vueCat = vueListe!
        service.getCategorie(ids: elem!)
        navigationController?.pushViewController(vueCat, animated: true)
        vueListe.navigationItem.title = "Groupe View";

使用此代码我得到以下错误:

"Pushing the same view controller instance more than once is not supported"

如何多次推送同一个 ViewController?

最佳答案

您应该创建一个 UIViewController 对象的新实例并推送它。

因此,在您的代码中,它将类似于:

case "Groupe":
    service.getGroupe()
    let vueGroupe = VueListeViewController() // I would create a new view controller here as well
    navigationController?.pushViewController(vueGroupe, animated: true)
    vueListe.navigationItem.title = "Groupe View";
case "Categorie":
    let vueCat = VueListeViewController() // New instance
    service.getCategorie(ids: elem!)
    navigationController?.pushViewController(vueCat, animated: true)
    vueListe.navigationItem.title = "Groupe View";

关于ios - 如何多次推送同一个 ViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56756548/

相关文章:

swift - 创建对象时的 ViewController 类

iphone - 如果可用,释放 UIViewController

ios - 如何以编程方式更改 UIDatePicker 的选定日期颜色 - Swift 4?

ios - 自动布局警告 tableview 单元格

ios - 在 Swift dequeueReusableCellWithIdentifier 中,需要时间来加载

ios - Realm 文件大小因保存相同数据而异

ios - UIPickerView - 第一行选择不调用 didSelectRow(在 Swift 中)

ios - UIViewController 基类——好不好

ios - 发送带有查询字符串的阿拉伯语数据

ios - iOS中的背景图像/视频上传