swift 3 : adding a UIAlertController in a SKView

标签 swift uiviewcontroller sprite-kit uialertcontroller

注意:

已经有几个与此相关的问题(或资源):

所以我很抱歉添加了类似的问题。但是,我尝试实现这些解决方案,但没有成功。

为了使这个问题比前面提到的问题更具体一些,让我们使用 M.W.E. ( Stack Overflow SKSpriteKit ) 在另外两个 Swift 相关问题中提出:

上下文:

M.W.E. 主要有一个UIViewController , GameViewController调用主菜单场景,即 SKView 。各种定制SKView s 循环链接:

菜单 -> 难度 -> 游戏 -> 得分 -> 菜单 -> 等等

我提到这一点,是因为在上述大多数问题中,必须添加 UIAlertController转到主UIViewController 。我只是想确保只有 GameScene可以呈现UIAlertController没有其他场景。

问题:

我如何实现UIAlertController只能从 SKView 之一调用是?之前的一些答案,只有主要的GameViewControllerGameScene ,然后给出GameScene ( GameViewController 中的常量)对 GameViewController 的引用。由于在此代码中,GameScene直到很久以后才被访问,如何实现这一点(理想情况下不通过各种 GameViewController 传播对 SKView 的引用。

请使用 M.W.E.回答这个问题的时候。

目标:

在另一个相关问题中, Swift3: UISwipeGestureRecognizer in only one of putatively many SKViews ,它提出了如何实现UIGestureRecognizer一个SKView 。理想情况下,UIAlertController将在该操作之后呈现 - 作为游戏中的菜单。

最小工作示例

StackOverflow SKSpriteKit

最佳答案

UIAlert 是一个 UIKit 元素,因此您可以轻松地在 SKScene 中呈现您想要的位置,只需很少的代码即可从您的 View 中获取引用.. 换句话说,您可以访问显示应用内容的主窗口,然后使用提供窗口内容 View 的rootViewController

代码示例:

if sprite.contains(touchLocation) {
            print("You tapped the blue sprite")
            let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
            let action = UIAlertAction(title: "Ok", style: .default) { action in
                // Handle when button is clicked
            }
            alert.addAction(action)
            if let vc = self.scene?.view?.window?.rootViewController {
               vc.present(alert, animated: true, completion: nil)
            }
}

关于 swift 3 : adding a UIAlertController in a SKView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41823096/

相关文章:

ios - 在 xcode 上使用 sprite kit 创建黑洞

ios - 在 iOS 应用程序启动序列中显示/删除启动图像的位置

ios - 从 TabBar 注销

ios - 为什么 playground 在 getter 结果前加上 "some"

ios - 为单个显示的 ViewController 在 iOS 7 上禁用自动旋转

iOS 嵌套 View 层次结构支持拖放功能以允许最终用户配置表单

ios - 从 UIImage 创建 CGPath

arrays - 在 Swift 的 indexPath 中保存 TableView 数组

ios - 错误——Command/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc 失败,退出代码为 1

ios - 使用 RxSwift/RxCocoa 订阅 UINavigationController 后退按钮