简体   繁体   中英

Navigate from a SKScene to a UIViewController

I want to navigate from a SKScene to a UIViewController . My code is as follows. However, when the method showViewController() is clicked the view doesn't get navigated. How can I solve this ?

I am using SWIFT3 and XCODE 8.1

class GameScene: SKScene, SKPhysicsContactDelegate {
    func showViewController() {
        print("button clicked")

        self.view!.window!.rootViewController!.performSegue(withIdentifier: "DashboardVIewControllerSegue", sender: self)

    }

}

What i have done to acheive this without having memory issue is.

first create some protocol(delegate) in the root viewController. This viewcontroller Contains the view as SKView where we load skscene.

so whenever you want to open a new viewcontroller from the skscene just call the protocol.

here is some code In 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)
    }
}

I have this code in maincontroller for popup alert when needed. within in gamescene.

and in game scene

  func showViewController() {
        let viewe = self.view as! GameSceneView
        viewe.myDelegate?. displayViewController()
    }

Hope you get this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM