关闭推送的视图控制器

15

我有一个视图控制器,它是这样显示的:

func showProfileForTrainer(trainer: Trainers) {
    let viewPTProfileVC = ViewPTProfileVC()
    viewPTProfileVC.trainer = trainer
    self.navigationController?.pushViewController(viewPTProfileVC, animated: true)
}

但是当我尝试关闭视图时,无法让它起作用。它在导航栏中有一个返回按钮,功能良好,但是当尝试通过按钮关闭视图时,它什么也不做。我目前已经尝试过:

func handleMessageTrainer() {
    dismiss(animated: true) {
        print(1)
        self.tabBarVC?.showChatLogForTrainer(trainer: self.trainer!)
    }
    self.dismiss(animated: true) {
        print(2)
        self.tabBarVC?.showChatLogForTrainer(trainer: self.trainer!)
    }
    navigationController?.dismiss(animated: true, completion: {
        print(3)
        self.tabBarVC?.showChatLogForTrainer(trainer: self.trainer!)
    })
    self.navigationController?.dismiss(animated: true, completion: {
        print(4)
        self.tabBarVC?.showChatLogForTrainer(trainer: self.trainer!)
    })
    print(5)
}

你可以看到,我尝试了各种方法,但都不起作用,控制台只输出5

令人沮丧的是,在我的应用程序其他地方,我以与开始时相同的方式呈现了一个视图,并使用dismiss(animated: true) { ... }正确地解除了它。

有任何想法为什么这里不起作用吗?

2个回答

47
您必须从相应的导航控制器中 弹出 视图控制器:

您必须从相应的导航控制器中 弹出 视图控制器:

self.navigationController?.popViewController(animated: true)

10

如果你正在使用pushviewcontroller方法,那么要dismiss它,你需要使用popviewcontroller方法

试试这个:

self.navigationController?.popViewController(animated: true)

现在我感到有点傻! 是否有一种方法可以使用完成方法弹出视图,就像使用dismiss一样? - Doto Pototo
PopViewController没有完成方法。 - Hiren Patel
但是您可以检查viewwilldisappear方法。 - Hiren Patel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接