我的游戏中有10个UIViewControllers或Views . 所有其他视图控制器都是第一个UIViewController的子类,在我的例子中是ViewControllerA,如下所示 . 我从每个视图或场景使用“home”功能转换到我的主菜单视图或场景 . 我还创建并添加主页按钮以使用函数“createHomeButton”以编程方式查看,该函数只是第一个视图控制器的一部分,因此在每个视图控制器子类中都可用 .

class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

}
// Create and add to the scene home button
func createHomeButton () {
    let imageButton = UIImage(named: "home_button")
    let button = UIButton(frame: CGRect(x: 680, y: 650, width: 120, height: 100))
    button.setImage(imageButton, for: UIControlState.normal)
    button.addTarget(self, action: #selector(home(_:)), for: .touchUpInside)
    view.addSubview(button)
}

// Dismiss current view controller and transition to Main Menu scene
func home(_ sender: AnyObject) {
    NSLog("Transition to Main Menu scene")
    let viewController = self.view?.window?.rootViewController
    viewController!.dismiss(animated: true, completion: nil)
}

}

然后在我的ViewControllerB和其余视图控制器(它们都是ViewcontrollerA的子类)中,我不创建主页按钮,因为我已经在ViewControllerA中创建了它,现在可用于每个子类视图 .

class ViewControllerB: ViewControllerA {
  override func viewDidLoad() {
  super.viewDidLoad()
  // Do any additional setup after loading the view, typically from a nib.

}

使用此设置,当我在子类ViewControllerB中并单击Home Button时,它首先转换到ViewControllerA视图,然后转到Main Menu视图 . 当按下Home按钮时,基本上从每个视图控制器子类,我将转换到ViewControllerA视图,然后转到主菜单视图 .

有没有办法进行这样的设置,以便当我在任何子类视图控制器中直接转换到主菜单场景而不是然后转到第一个视图然后到主菜单场景?