Swift3:在 SKView 中添加一个 UIAlertController
Swift3: adding a UIAlertController in a SKView
注:
已经有几个与此相关的问题(或资源):
-
How would I create a UIAlertView in Swift?
所以我很抱歉添加了一个类似的问题。但是,我尝试实施这些解决方案但没有成功。
为了使这个问题比上述问题更具体一点,让我们使用 M.W.E。 (Stack Overflow SKSpriteKit) 在另外两个 Swift 相关问题中提出:
- Swift: SKSpriteKit, using Storyboards, UIViewController and UIButton to set in game parameters?
上下文:
在M.W.E.
有一个主UIViewController
,GameViewController
调用主菜单场景,这是一个SKView
。各种自定义SKView
s link循环:
菜单 -> 难度 -> 游戏 -> 分数 -> 菜单 -> 等
之所以提这个,是因为在上面的大部分问题中,必须在主UIViewController
的基础上加上UIAlertController
。我只是想确保只有GameScene
可以呈现其他场景的UIAlertController
和none。
问题:
如何实现仅从 SKView
之一调用的 UIAlertController
?前面的一些回答,只有主要的GameViewController
和GameScene
,然后给GameScene
(GameViewController
中的一个常量)引用了GameViewController
].由于在此代码中,GameScene
直到很晚才被访问,如何实现这一点(理想情况下不通过各种 SKView
s 传播对 GameViewController
的引用。
请在回答这个问题时使用M.W.E.。
目标:
在另一个相关问题中,
Swift3: UISwipeGestureRecognizer in only one of putatively many SKViews,它提出了如何为一个SKView
实现一个UIGestureRecognizer
。理想情况下,UIAlertController
将在该操作之后显示 - 作为游戏中的菜单。
最小工作示例
看起来是个很长的问题。你想在 SKScenes 中显示一个 UIAlertController 吗?无需从您的 SKScenes 引用 GameViewController,只需在根视图控制器上显示警报。
所以要直接从您的 SKScenes 之一进行演示,您可以这样说
view?.window?.rootViewController?.present(YOURALERTCONTROLLER, animated: true)
希望对您有所帮助
UIAlert
是一个 UIKit
元素,因此您可以轻松地在 SKScene
中显示您想要的位置,只需很少的代码即可从您的视图中获取引用。
换句话说,您可以访问显示应用程序内容的主要 window
,然后使用提供 window.
内容视图的 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)
}
}
注:
已经有几个与此相关的问题(或资源):
How would I create a UIAlertView in Swift?
所以我很抱歉添加了一个类似的问题。但是,我尝试实施这些解决方案但没有成功。
为了使这个问题比上述问题更具体一点,让我们使用 M.W.E。 (Stack Overflow SKSpriteKit) 在另外两个 Swift 相关问题中提出:
- Swift: SKSpriteKit, using Storyboards, UIViewController and UIButton to set in game parameters?
上下文:
在M.W.E.
有一个主UIViewController
,GameViewController
调用主菜单场景,这是一个SKView
。各种自定义SKView
s link循环:
菜单 -> 难度 -> 游戏 -> 分数 -> 菜单 -> 等
之所以提这个,是因为在上面的大部分问题中,必须在主UIViewController
的基础上加上UIAlertController
。我只是想确保只有GameScene
可以呈现其他场景的UIAlertController
和none。
问题:
如何实现仅从 SKView
之一调用的 UIAlertController
?前面的一些回答,只有主要的GameViewController
和GameScene
,然后给GameScene
(GameViewController
中的一个常量)引用了GameViewController
].由于在此代码中,GameScene
直到很晚才被访问,如何实现这一点(理想情况下不通过各种 SKView
s 传播对 GameViewController
的引用。
请在回答这个问题时使用M.W.E.。
目标:
在另一个相关问题中,
Swift3: UISwipeGestureRecognizer in only one of putatively many SKViews,它提出了如何为一个SKView
实现一个UIGestureRecognizer
。理想情况下,UIAlertController
将在该操作之后显示 - 作为游戏中的菜单。
最小工作示例
看起来是个很长的问题。你想在 SKScenes 中显示一个 UIAlertController 吗?无需从您的 SKScenes 引用 GameViewController,只需在根视图控制器上显示警报。
所以要直接从您的 SKScenes 之一进行演示,您可以这样说
view?.window?.rootViewController?.present(YOURALERTCONTROLLER, animated: true)
希望对您有所帮助
UIAlert
是一个 UIKit
元素,因此您可以轻松地在 SKScene
中显示您想要的位置,只需很少的代码即可从您的视图中获取引用。
换句话说,您可以访问显示应用程序内容的主要 window
,然后使用提供 window.
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)
}
}