Share
Explore

IOS Lab Book: The Guessing Game App

Objective:
The objective of this lab workbook is to teach students how to create a guessing game iOS app. The app will generate a random number between 1 and 100 and ask the user to guess the number. The app will provide feedback on whether the guess is too high or too low and allow the user to keep guessing until they guess the correct number.
Prerequisites:
Familiarity with basic programming concepts
Xcode installed on your computer
Apple Developer account (free or paid)

Step 1: Create a new Xcode project

Open Xcode and click on "Create a new Xcode project"
Select "App" under "iOS" and click "Next"
Choose "Single View App" and click "Next"
Enter a name for the project, select a location to save it, and click "Create"

Step 2: Design the user interface


Open the "Main.storyboard" file
Drag a "Label" and a "Text Field" onto the screen
Position the label and text field so that they are centered horizontally and vertically on the screen
Change the text of the label to "Guess the number"
Change the placeholder text of the text field to "Enter your guess"
Drag a "Button" onto the screen and position it below the text field
Change the text of the button to "Guess"

Step 3: Create IBOutlets and IBActions

Open the "ViewController.swift" file
Ctrl-click and drag from the label to the top of the file to create an IBOutlet called "numberLabel"
Ctrl-click and drag from the text field to the top of the file to create an IBOutlet called "guessTextField"
Ctrl-click and drag from the button to the top of the file to create an IBAction called "guessButtonPressed"

Step 4: Add game logic

In the "guessButtonPressed" function, generate a random number between 1 and 100 using the following code:
let randomNumber = Int.random(in: 1...100)


Convert the user's guess from the text field to an integer using the following code:
guard let guess = Int(guessTextField.text!) else {
return
}


Compare the user's guess to the random number and provide feedback using the following code:

if guess < randomNumber {
numberLabel.text = "Too low!"
} else if guess > randomNumber {
numberLabel.text = "Too high!"
} else {
numberLabel.text = "You win!"
}


Step 5: Test the app


Connect your iOS device to your computer or use the simulator
Click on the "Play" button in Xcode to build and run the app
Test the app by entering guesses and checking the feedback

Conclusion:

In this lab workbook, we learned how to create a guessing game iOS app using Xcode.
We designed the user interface, added IBOutlets and IBActions, and added game logic to generate a random number and provide feedback to the user based on their guess.
This lab workbook should serve as a foundation for more complex iOS app development.



Working with the Split Editor, and connecting buttons and fields to IBOutlets and Actions:

steps to drag a button to an IBOutlet in Xcode.

1. Open your Xcode project and navigate to the storyboard where your button is located.
2. Open the Assistant Editor by clicking on the icon in the top right corner of Xcode or by pressing `Command + Option + Return`.
3. Make sure that both the storyboard and view controller files are visible in the Assistant Editor.
4. Ctrl+drag from your UIButton on the storyboard to an empty line in your view controller file.
5. Release the mouse button when you see a blue line appear between your UIButton and view controller file.
6. In the pop-up window, give your IBOutlet a name and make sure that "Connection" is set to "Outlet".
7. Click "Connect" to create your IBOutlet.
Here's an example code snippet of what it would look like after creating an IBOutlet for a UIButton:
@IBOutlet weak var myButton: UIButton!
This creates an optional weak reference called myButton of type UIButton. You can now use this reference to manipulate properties of your UIButton programmatically by using the dot accessor notation. For example: myButton.text = “Click Me”

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.