Hi guys,
Please can someone help me with this…
I’m trying to create an app that generates a random number. On the firstViewController I have set a button (to generate the number) and label (to display the generated number) and on the secondViewController I have created a UI that allows the user to change the parameters of how the app generates a number. Right now I’m busy with the first. The Range - I want it to allow the user to increase the range the app can generate a random number from (between 2 - Users selected number(Max 10))
Now the problem I have is that the app doesn’t register the change in value of the range variable (var targets = 2). It keeps printing it as 2 no matter if I change the value with the UIButtons.
First View Controller Code
‘’’
import UIKit
class FirstViewController: UIViewController {
// MARK: Variables
var delegate: RoundEdited?
let d = SecondViewController()
// MARK: IB Outlets
@IBOutlet weak var startButton: UIButton!
@IBOutlet weak var mainLabel: UILabel!
// MARK: viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func getInfo() {
delegate?.editRound()
}
//MARK: Start Round
@IBAction func startRoundTapped(_ sender: Any) {
self.delegate = d
self.getInfo()
}
} // End of viewDidLoad
‘’’
Second View Controller Code
‘’’
import UIKit
protocol RoundEdited {
func editRound()
}
class SecondViewController: UIViewController, RoundEdited {
//MARK: Variables
var targets = 2
var calls = 1
//MARK: IB Outlets
// Targets Label
@IBOutlet weak var targetLabel: UILabel!
// Add & Subtract Target Buttons
@IBOutlet weak var addTargetButton: UIButton!
@IBOutlet weak var subtractTargetButton: UIButton!
// Calls Label
@IBOutlet weak var callsLabel: UILabel!
// Add & Subtract Call Buttons
@IBOutlet weak var addCallsButton: UIButton!
@IBOutlet weak var subtractCallsButton: UIButton!
// MARK: viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Load the view with the subtractTargetButton Disabled
subtractTargetButton.isEnabled = false
// Load the view with the subtractCallsButton Disabled
subtractCallsButton.isEnabled = false
// Set the targetLabel to the targets value
targetLabel.text = String(targets)
// Set the callsLabel to the calls value
callsLabel.text = String(calls)
}
//MARK: Add or Subtract Targets
// Subtract Targets
@IBAction func subtractTargetTapped(_ sender: Any) {
// Create the lower Limit (2)
if targets == 2 {
targets += 0
targetLabel.text = String(targets)
}
else {
targets -= 1
targetLabel.text = String(targets)
}
// Disable the subtractTargetButton at Lower Limit (2)
if targets == 2 {
subtractTargetButton.isEnabled = false
}
// Enable the addTargetButton when not at Upper Limit (10)
if targets < 10 {
addTargetButton.isEnabled = true
}
}
// Add Targets
@IBAction func addTargetTapped(_ sender: Any) {
// Create the Upper Limit (10)
if targets == 10 {
targets += 0
targetLabel.text = String(targets)
}
else {
targets += 1
targetLabel.text = String(targets)
}
// Enable subtractButton when not at Lower Limit (2)
if targets > 2 {
subtractTargetButton.isEnabled = true
}
// Disable the addTargetButton at Upper Limit (10)
if targets == 10 {
addTargetButton.isEnabled = false
}
}
//MARK: Add or Subtract Calls
// Subtract Calls
@IBAction func subtractCallsLabel(_ sender: Any) {
// Create the Lower Limit (1)
if calls == 1 {
calls -= 0
callsLabel.text = String(calls)
}
else {
calls -= 1
callsLabel.text = String(calls)
}
// Disable the subtractCallsButton at Lower Limit (1)
if calls == 1 {
subtractCallsButton.isEnabled = false
}
// Enable the subtractCallsButton when not at Upper Limit (15)
if calls < 15 {
addCallsButton.isEnabled = true
}
}
// Add Calls
@IBAction func addCallsLabel(_ sender: Any) {
// Create the Upper Limit (15)
if calls == 15 {
calls += 0
callsLabel.text = String(calls)
}
else {
calls += 1
callsLabel.text = String(calls)
}
// Enable the subtractCallsButton when not at Lower Limit (1)
if calls > 1 {
subtractCallsButton.isEnabled = true
}
// Disable the subtractCallsButton at the Upper Limit (15)
if calls == 15 {
addCallsButton.isEnabled = false
}
}
// MARK: Edit the Round
func editRound() {
let targetsSet = Int.random(in: 2...targets)
print(targetsSet)
}
} // End of viewDidLoad
‘’’
Please can someone help me understand why it’s not picking up the change. Thanks in advance!