1

I am a beginner programmer and have been trying to teach myself to program a basic app online. I am currently trying to make Tetris on iOS using Swift 2 and Xcode 7. However, I am having trouble changing views. I began with the iOS Game template and have a view named GameViewController which my game runs inside. When the user loses, I want to switch from that view to one called EndGameView, which I have created in the storyboard and linked to the class EndGameView. My issue is that I do not know how to present the EndGameView programmatically without using, for example, a button added in using the storyboard and then control-dragging to the EndGameView.

let endGameView = EndGameView()
super.presentViewController(endGameView, animated: true, completion: nil)

Please excuse my stupidity as I'm sure I'm making a dumb error and thanks for any help.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
liam923
  • 991
  • 1
  • 10
  • 19
  • can you show me where action you end game? And the name of storyboard of you? – vien vu Dec 24 '15 at 02:36
  • Is `EndGameView` a `UIView` subclass or a `UIViewController` subclass? If it's a view controller, it's helpful to name it as such to avoid confusion. If it's a view controller, the code you used should work, except you should change `super` to either `self` or a different view controller that you want `EndGameView` to be presented from. – Aaron Brager Dec 24 '15 at 02:39

1 Answers1

0

A simple way to change view controller is to create a segue between your two view controllers and call it when the player dies, basically going from your game view controller to your EndGameViewController. Here is a line of code to call a segue: self.performSegueWithIdentifier("SEAGUEIDTOLOGGEDINSCREEN", sender: nil) Replace SEAGUEIDTOLOGGEDINSCREEN with the segue you have created's id.

How to create a segue: Right click and drag from your GameViewController to your EndGameViewController and select "Show" enter image description here

After inking them, you should see a link, click on it and you should see on your side the id and name.

enter image description here

enter image description here

Add the name of the segue to the identifier slot, and use that on calling it.

Max Kortge
  • 527
  • 7
  • 23
  • When I did this is got the error "terminating with uncaught exception of type NSException." Am I using the wrong kind of segue/creating it incorrectly? – liam923 Dec 24 '15 at 02:38
  • Are you sure your SegueID is the same as it is defined, too check it just go to your main.storyboard and click on the link between the two view controllers and you should see the id. I'll edit my answer to have more detail on defining a segue. – Max Kortge Dec 24 '15 at 02:40
  • Never mind, I fixed it by trial and error. I used modal for the kind. Thanks for your help:) – liam923 Dec 24 '15 at 02:41
  • Is there a way to pass a variable to the EndGameView from the GameViewController? I would like the pass the user's score if possible. – liam923 Dec 24 '15 at 02:51
  • I'm not currently aware of any way, I've found a few question with answers about the same thing here on stackoverflow, check this out and if it doesn't solve your question you could always find other questions or ask your own. Here is the link to a previous question I have found: http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Max Kortge Dec 24 '15 at 03:18