Swift iOS: Perform a Segue from an Instance in a V

2019-08-15 01:45发布

First of all, I have to say that there is so much information on this platform, because of everyone, who is helping. Thank you very much.

At the moment, I have reached a point, where I am not able to go further on my own.

This is my problem:

I have a MainScreen ViewController which is embeded in a Navigation Controller. From this MainScreen, I can show with a Segue a GameViewController which creates a Gamescene. In this Gamescene, I have a button, which when it is tapped, shows a Popup and pauses the Gamescene.

This Popup contains two buttons:

  1. A button to resume the game and make the popup disappear.
  2. A button which should end the Gamescene and return to the Mainscreen.

And here is my problem. I am not able to create the function for the End Button. I have tried different constellations with segues, but it didn't end well.

I think that the difficulty here is, that this button is in an separate class. The hierarchy here, as I understand it, would be:

GameViewController -> GameScene -> Popup -> EndButton.

Do you have any ideas how I could solve this problem? Would there be an other way to solve this transition without using segues?

Thank you very much for your help in advance!

Kind regards, Phil

Update:

In my Gamescene the popup basically looks like this:

class GameScene: SKScene,  SKPhysicsContactDelegate {

  //  ....
  var pauseButton : UIButton! = UIButton()
  var pausePopup : PausePopup!    // my Popup class with the End button
  //  ....

  override func didMoveToView(view: SKView) {

    //configuring the button
    pause_button.addTarget(self, action: "pauseButtonPressed", forControlEvents: UIControlEvents.TouchUpInside)

    self.view?.addSubview(pause_button)

  }

  func pauseButtonPressed() {
    self.pausePopup = PausePopup(nibName: "uxPausePopup", bundle: nil)
    self.scene!.view!.paused = true
    self.pausePopup.showInView(self.viewPlaceHolder, animated: true, scene: self)
  }
    // this is where my popup appears.

In my Popup class I have an action for the EndButton which is located on the Popup and from which I would like to return to the main screen:

@IBAction func showMainScreen(sender: AnyObject) {
   // this does not work ...
   self.performSegueWithIdentifier("showMainScreen", sender: nil)
}

1条回答
等我变得足够好
2楼-- · 2019-08-15 02:27

You cannot call other view from showInView, you need to dismiss the popOver and call the mainView from GameScene, to do so you can create a competition handler in your PausePopup or a protocol to pass the result of your popOver to GameScene deal with the result

查看更多
登录 后发表回答