0

Using Xcode 7.3, writing Swift program; searched for this topic but could not find an answer. Apologies, in advance. Last program language was COBOL.

Application background: It is a single view application, User keys in data and at some point the User 'wins' - at that point, two buttons are available: 'Quit?' and 'Play Again?'

For the 'Quit?' button I have:

// MARK: C.This IBAction used to quit the game

@IBAction func quitIt(sender: AnyObject)

{
        exit(0)
}

Hopefully, the above is the most efficient way.

What I need help on is the 'Play Again?' button:

// TODO: B. This IBAction used to play another game

@IBAction func Playagain(sender: AnyObject)
{

}

What code do I put in?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Joe
  • 1
  • 1
  • 1

1 Answers1

0

Move the startup code for your game to it's own func(tion).

Call that function both from [wherever you moved it from] and also in PlayAgain().

I hope this is for OS X rather than iOS - Apple will probably reject your iOS app if it exits. They consider that "crashing". https://developer.apple.com/library/ios/qa/qa1561/_index.html

Steve Barron
  • 944
  • 1
  • 6
  • 15
  • @ Steve Barron Thank you for replying. Not sure what you mean by 'startup code'. Sorry for being so dense. I plan to run it on my IPad and on the MacBook Pro. – Joe Apr 16 '16 at 13:50
  • When you first start the app/game, I'm assuming there is some code which sets up the initial game...sets the initial score to 0, resets the number of lives, creates a new puzzle, etc., depending on the kind of game it is. That code needs to move from the ViewController's viewDidLoad (if that's where you put it) into it's own function so that you can call it from viewDidLoad but ALSO from the Playagain IBAction. – Steve Barron Apr 17 '16 at 19:10
  • The other comment (also made by others) about iOS v OS X is that iOS apps generally do not close themselves the way we might on OS X or Windows. iOS apps are meant to live forever until the user kills them manually. So, in the iOS version, you might want to get rid of the Quit button - but it's probably OK in the OS X version. Check that link in my answer for the source/details of that rule from Apple. – Steve Barron Apr 17 '16 at 19:13
  • @ Steve Barron I do have an initialization function ("get5040" sitting by itself just after the 'did.ReceiveMemoryWarning' code. In the 'viewDidLoad' area the 'get5040' function is called: "get5050()". – Joe Apr 18 '16 at 21:57
  • @ Steve Barron I do have an initialization function ("get5040" sitting by itself just after the 'did.ReceiveMemoryWarning' code. In the 'viewDidLoad' area the 'get5040' function is called: "get5050()". Is this location of the initialization code a 'bad practice'? Should the initialization code all be in the 'viewDidLoad' area? I currently have all my initializaton variables sandwiched between 'Import UIKit' and 'Class ViewController'. Should all of these variables be moved to the "get5040" function? – Joe Apr 18 '16 at 22:05
  • Whatever needs to happen to get the user back to a new game state should all happen in get5050, whether this is the first game (called from viewDidLoad) or a replay (called from Playagain). Variable declaration makes sense in the controller itself, as you describe, but setting things back to 0 needs to happen in get5050. – Steve Barron Apr 19 '16 at 12:31
  • Followed your suggestion, everything works. Thank you. New problem: the User keys in a 4 digit number - is there anyway to restrict the input area to just digits (I have checked 'Number Pad' but I still get the whole keyboard coming up) and can the input area have a limit of 4 key strokes (or 4 'looping' on itself) - Joe – Joe Apr 21 '16 at 04:47
  • Hey, Joe. So, the way Stack Overflow generally works is that each question is it's own thing. In fact, I bet some folks would say that we had WAY too much conversation on this one. :) Would you mind accepting my answer as the correct one (if you still think it is) and then asking that new question separately? I'll keep an eye out for it but a new question will ALSO attract more helpers. In the meantime, I'm a little surprised that setting the Text Field to use the numeric keypad didn't do what you wanted. – Steve Barron Apr 21 '16 at 22:44
  • http://stackoverflow.com/questions/27215495/limit-uitextfield-input-to-numbers-in-swift – Steve Barron Apr 21 '16 at 23:07