Reputation: 137
I am trying to pass a string from one if statement to another. I've declared the var outside of both if statements with just place holder strings for now.
What should be happening is that a button is tapped once and it displays the title of the button (think buttons/numbers on a calculator). When the button is tapped a second time it should display both the title of the first button press plus the title of the second button pressed in order to create a two-digit number.
What is actually happening is that it correctly displays button number one, but once the second button is hit it displays button number one as the place holder string.
The code is below. Any help would be great!
Screenshot of app + code.. It should read "21" next to new card instead of "place 1" https://i.sstatic.net/qNM0e.jpg
@IBAction func oneButtonPressed(sender: AnyObject)
{
timesButtonPressed += 1
var buttonTitle1:String! = ("Place ")
var buttonTitle2:String! = ("holder")
if timesButtonPressed == 1
{
buttonTitle1 = sender.titleLabel!?.text
self.answerTyped.text = String (buttonTitle1)
}
if timesButtonPressed == 2
{
buttonTitle2 = sender.titleLabel!?.text
self.answerTyped.text = String (stringInterpolation: buttonTitle1, buttonTitle2)
}
}
FULL CODE:
import UIKit
class PlayViewController: UIViewController {
@IBOutlet weak var newCardButton: UIButton!
@IBOutlet weak var labelTest: UILabel!
@IBOutlet weak var oneButton: UIButton!
@IBOutlet weak var answerTyped: UILabel!
@IBOutlet weak var enterButton: UIButton!
var mathProblem = String ("Answer Typed")
var firstRandomNumber = 0
var secondRandomNumber = 0
var buttonName = String ("button name")
var timesButtonPressed = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func newCardButtonPressed(sender: AnyObject)
{
firstRandomNumber = Int(arc4random_uniform(11))
secondRandomNumber = Int(arc4random_uniform(11))
mathProblem = String(format:"%i x %i", firstRandomNumber, secondRandomNumber)
self.labelTest.text = String (mathProblem)
}
@IBAction func oneButtonPressed(sender: AnyObject)
{
timesButtonPressed += 1
var buttonTitle1:String! = ("Place ")
var buttonTitle2:String! = ("holder")
if timesButtonPressed == 1
{
buttonTitle1 = sender.titleLabel!?.text
self.answerTyped.text = buttonTitle1
}
if timesButtonPressed == 2
{
buttonTitle2 = sender.titleLabel!?.text
self.answerTyped.text = buttonTitle1 + buttonTitle2
}
}
Upvotes: 0
Views: 187
Reputation: 121
Problem is when you press button second time then timesButtonPressed == 1, never executed and and your timesButtonPressed == 2 if block takes value for title1 from code instead of actual button1 title value.
var timesButtonPressed : NSInteger = 0
var displayString : String = "0"
@IBAction func oneButtonPressed(sender: AnyObject)
{
timesButtonPressed += 1
let buttonTitle:String! = sender.titleLabel!?.text
if timesButtonPressed == 1
{
displayString = buttonTitle
} else {
displayString += buttonTitle
}
self.answerTyped.text = displayString
}
timesButtonPressed var need to use as needed (or clear display string when timesButtonPressed = 0)
Upvotes: 1
Reputation: 9540
Edited:
You need to define both the variable outside of the IBAction function. Put the below two variable in viewDidLoad()
var buttonTitle1:String! = ("Place ")
var buttonTitle2:String! = ("holder")
You can replace add two string using by overloading "+":
var str1: String = "Hello"
var str2: String = "World"
var str3: String = str1 + str2
Similarly, you can do that in your function:
self.answerTyped.text = buttonTitle1 + buttonTitle2
Upvotes: 0