Swift - 如何编写一个按钮以转到主场景,而无需返回?

Swift - How to program a button to go to the mainScene, without segues going back?

我正在制作一个游戏,当您按下 开始按钮 时,它会将您带到 游戏场景 然后游戏是上方重播按钮将出现在屏幕上,当按下重播按钮时它将带我到主屏幕NOT 在我的游戏场景中。浏览了几个小时后,我发现了一些与我要找的东西相关的东西,但它在 objective-c 中。 objective-C 中的代码在下面,请告诉我 swift 中的代码是什么意思,因为我根本不熟悉 objective-c。

(IBAction)prepareForUnwind:(UIStoryboardSegue*)sender
{
    UIViewController *sourceViewController = sender.sourceViewController;

    // Pull any data from the view controller which initiated the unwind segue.
}

这是您的 swift 代码:

@IBAction func prepareForUnwind(segue: UIStoryboardSegue) {
    let sourceViewController = sender.sourceViewController
}

除了@Dharmesh Kheni 的回答

您还可以通过编程方式调用 viewcontroller,无需转场。

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("SomeID") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)

您可以为您需要展示的 viewcontroller 设置标识符,如下所示:

希望对您有所帮助。