[英]Navigate from a SKScene to a UIViewController
我想从SKScene
导航到UIViewController
。 我的代码如下。 但是,当单击方法showViewController()
,视图不会被导航。 我怎么解决这个问题?
我使用的是SWIFT3
和XCODE 8.1
class GameScene: SKScene, SKPhysicsContactDelegate {
func showViewController() {
print("button clicked")
self.view!.window!.rootViewController!.performSegue(withIdentifier: "DashboardVIewControllerSegue", sender: self)
}
}
我没有遇到记忆问题就能做到这一点。
首先在根viewController中创建一些协议(委托)。 这个viewcontroller包含视图作为我们加载skscene的SKView。
所以每当你想从skscene打开一个新的viewcontroller时,只需调用协议即可。
这是mainViewcontroller中的一些代码:
protocol GameProtocol {
func displayViewController()
}
。
extension MainViewController: GameProtocol {
internal func displayViewController() {
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let popoverVC = storyboard.instantiateViewController(withIdentifier: "SettingViewController") as! SettingViewController
// popoverVC.modalPresentationStyle = .fullScreen
// popoverVC.modalPresentationStyle = .popover
popoverVC.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
popoverVC.view.backgroundColor = UIColor.popBackgroundColor
popoverVC.modalPresentationStyle = .popover
popoverVC.popoverPresentationController!.delegate = self
popoverVC.popoverPresentationController!.sourceView = self.view
popoverVC.popoverPresentationController!.sourceRect = CGRect(x: 0.5, y: 0.5, width: 0, height: 0)
popoverVC.preferredContentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height)
self.present(popoverVC,animated: false,completion: nil)
}
}
我在maincontroller中有这个代码,需要时弹出警报。 在gamecene中。
在游戏场景中
func showViewController() {
let viewe = self.view as! GameSceneView
viewe.myDelegate?. displayViewController()
}
希望你明白这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.