ios - 在 Swift 中显示以前创建的 View Controller

标签 ios swift

我通常会使用 segue 来切换 View Controller ,但我不认为我可以使用 segue 来完成我正在做的事情。当游戏结束时,我会出现一个警告,告诉用户游戏已经结束。用户可以在警报 View 上点击两个按钮。一个是“再试一次”,另一个是“主菜单”。当用户点击“主菜单”时,它会切换到主视图 Controller 。但是,当我使用“instantiateViewControllerWithIdentifier”时,每次用户点击“主菜单”时,它都会创建该主视图 Controller 的新实例。如何将警报 View 中的“主菜单”按钮切换到主视图 Controller 的原始实例?这是我的代码:

gameOverAlert.addAction(UIAlertAction(title: "Main Menu", style: .Default, handler: { (action: UIAlertAction!) in
        self.ReturnToMainMenu()
    }))

func ReturnToMainMenu(){
    var storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

    let Main = self.storyboard!.instantiateViewControllerWithIdentifier("Main") as! ViewController

    self.presentViewController(Main, animated:true, completion:nil)
}

最佳答案

关闭您呈现的 View Controller (您的游戏),以便它显示您之前的 View Controller (您的主菜单)。

在您返回主菜单时,self.dismissViewControllerAnimated(false, completion: nil)

关于ios - 在 Swift 中显示以前创建的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32077943/

相关文章:

IOS 关闭应用程序时丢失 FBSession

ios - 家庭应用程序图标在我的 iphone 6 设备上没有正确显示。全黑

ios - 如何使用 SwiftyJSON 发出 post 请求

swift - 在非类绑定(bind)协议(protocol)的扩展中,实例必须被视为值类型

ios - 使用 Swift 解析推送时没有声音

ios - 将 UIButtons 添加到 ViewDidLoad 上的数组

ios - UITableViewCell 可重用性问题。修改一个细胞会影响其他细胞

ios - Snap Kit 不使用 startSending 打开 Snapchat

swift - 遍历对象的属性(在 Realm 中,也可能不在)

ios - 使用 AVFoundation 裁剪 AVAsset 视频不适用于 iOS 8