来自另一个 viewcontroller 的 Performsegue 不起作用

Performsegue from another viewcontroller doesnt' work

到目前为止我一直在使用 objective-C 并决定从现在开始使用 swift 进行开发。

我不知道为什么这个简单的代码不起作用。

我有两个viewController作为父视图的容器视图,一个叫做'sideViewController',另一个是'dashViewController'。

我有一个标识符为 'Appointment' 的 segue,从 dashViewController 到另一个名为 'nextViewController'

的 viewController

如果我尝试在 dashViewController.swift 中执行转义,它工作正常。

但为什么我尝试在 'sideViewController' 中使用它却不起作用?

@IBAction public func buttonTapped(_ sender: Any) {
    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyBoard.instantiateViewController(withIdentifier: "dashboardViewController")
    vc.performSegue(withIdentifier: "Appointment", sender: self) //nothing happens
}

在 objective C 中,我可以像下面那样为 dashViewController 使用单例

[[MainViewController sharedViewController] performSegueWithIdentifier:@"Appointment" sender:nil];

但在 swift 中,以下代码不起作用

DashboardViewController.sharedInstance.performSegue(withIdentifier: "Appointment", sender: nil)

我已经为此苦苦挣扎了 2 天..

请帮助我。

谢谢

在您的 buttonTapped() 函数中,您正在创建 "dashboardViewController" 的 new 实例,然后尝试执行转场……但是您还没有将新的 VC 或其视图添加到层​​次结构中,因此即使要执行转场 ,也没有什么可见。

您要做的是使用协议/委托模式。

创建一个协议,这样您在 "sideViewController" 中点击按钮可以告诉它的委托("main" 视图控制器)告诉 现有的 实例 "dashboardViewController" 执行转场。

协议和委托在 Swift 中的功能与 Obj-C 中的相同...只是语法不同。