Adding a UI to the Bingo Machine
- Create a new Project using the iOS Single View Application template
- Set the language to Swift, and the User Interface to Storyboard. Name the product something like BingoCaller
- Click on the project name in the project navigator, and under deployment info, ensure only the landscape options are checked.
- Add the BingoMachine.swift file you created previously to the project, either by dragging them into your project, or by using the menu option File -> Add files to BingoCaller, ensuring you select the checkboxes as shown below:
- Open the storyboard and with the ViewController selected, at the bottom of the window, set the size to the smallest iPad and the orientation to landscape.
- Towards the right hand side of the view, add a label and two buttons, Set the label to display two dashes, centre its text and enlarge the font. Ensure it has enough space to display 2 numbers. Set the button titles to 'Reset' and 'Call'.
- Connect up the label as an outlet named calledNumber.
- Connect the buttons up as actions named "callNumberPressed" and "resetPressed" appropriately. ViewController's interface will now contain code similar to that shown below
@IBOutlet weak var calledNumber: UILabel!
@IBAction func resetPressed(sender: AnyObject) {
}
@IBAction func callNumberPressed(sender: AnyObject) {
}
- In the ViewController class (outside any of it's functions), add the following to create a BingoMachine:
var bingoMachine = BingoMachine()
-
Inside the
callNumberPressed
method, add the following code to get a number from the bingo machine and display it in the calledNumber label:
let ballNumber = bingoMachine.getNumber()
calledNumber.text = String(ballNumber)
- Run the application and you should be able to call 90 numbers, although if you call the 91st number the application will crash.
- Add code inside the callNumberPressed method to only call the number if there are balls remaining.
- Finally, add code to reset the Bingo machine if the reset button is pressed. This should be as simple as re-initialising the bingoMachine variable.