Swift - 如何使用后退按钮在故事板之间切换?

Swift - How switch between storyboards with back button?

我在一个情节提要中,我需要点击一个按钮转到另一个情节提要。好吧,这我已经完成了,但我需要回到之前的故事板。这是我的代码:

func goContact() {
    let storyboard = UIStoryboard(name: "FAQ", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "ContactViewController")
    self.navigationController?.show(vc, sender: nil)
}

通过点击我的 UITableViewCell 上的一个按钮(我有一个协议),这个函数 (goContact) 被调用,我切换到另一个故事板 (FAQ)。但是,我怎样才能回到main.storyboard?

我也试过这样做:

func goContact() {
    let storyboard = UIStoryboard(name: "FAQ", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "ContactViewController")
    self.navigationController?.show(vc, sender: nil)
    // self.navigationController?.pushViewController(vc, animated: true)
    // self.present(vc, animated: true, completion: nil)
}

仅使用 storyboard 属性 ,它引用应用程序的主要故事板,或者您可以像 let storyboard = UIStoryboard(name: "Main", bundle: nil)

这样写

推送完成后返回

self.navigationController?.popViewController(animated: true)

您应该使用推送将新的视图控制器推送到导航控制器上,就像在注释掉的代码中一样。这将导致新的视图控制器有一个后退按钮,它会为您弹出它而不需要任何额外的代码。