How do I set up the buttons that are linked to didPressNumber to add to each other when pressed - ios

How do I set up the buttons that are linked to didPressNumber to add to each other when pressed so lets say its a calculator and I want set it up where each button is pressed has a letter and number value when it is pressed it adds to the previous one press and I want to set up 2 labels one displaying the number value and one displaying the letter value and how would I set up the value of each number?
enum modes {
case not_set
case addition
case subtraction
case equals
}
#IBAction func didPressNumber(_ sender: UIButton) {
let stringValue:String? = sender.titleLabel?.text
if (lastButtonWasMode) {
lastButtonWasMode = false
labelString = "0"
}
labelString = labelString.appending(stringValue!)
updateText()
}
func updateText() {
guard let labelInt:Int = Int(labelString) else {
return
}
if (currentMode == .not_set) {
savedNum = labelInt
}
let formatter: NumberFormatter = NumberFormatter()
formatter.numberStyle = .decimal
let num:NSNumber = NSNumber(value: labelInt)
label.text = formatter.string(from: num)
}
func changeMode(newMode:modes) {
if (savedNum == 0) {
return
}
currentMode = newMode
lastButtonWasMode = true
}

Related

How to make a full decimal point for a calculator on swift?

I've just started to study Xcode.
I've made all digits and math signs, but have no clue how to make a dot for calculator.
This is what I've done (deleted some repeating parts of math operations):
class ViewController: UIViewController {
var numberFromScreen: Double = 0
var firstNum: Double = 0
var operation: Int = 0
var mathSign: Bool = false
#IBOutlet weak var result: UILabel!
#IBAction func digits(_ sender: UIButton) {
if mathSign == true {
result.text = String (sender.tag)
mathSign = false
}
else {
result.text = result.text! + String (sender.tag)
}
numberFromScreen = Double (result.text!)!
}
#IBAction func buttons(_ sender: UIButton) {
if result.text != "" && sender.tag != 10 && sender.tag != 15 {
firstNum = Double (result.text!)!
if sender.tag == 11 {// divine
result.text = "/"
}
operation = sender.tag
mathSign = true
}
else if sender.tag == 15 {// calculate
if operation == 11 {
result.text = String(firstNum / numberFromScreen)
}
}
else if sender.tag == 10 {
result.text = ""
firstNum = 0
numberFromScreen = 0
operation = 0
}
}
}
In your case using the NumberFormatter would be a good option.
You can define it as follows:
private var formater: NumberFormatter {
let formater = NumberFormatter()
formater.maximumIntegerDigits = 9 // Change this value
formater.maximumFractionDigits = 9 // Change this value
formater.minimumFractionDigits = 9 // Change this value
formater.minimumIntegerDigits = 1 // Change this value
formater.maximumIntegerDigits = 9 // Change this value
formater.groupingSeparator = " "
formater.locale = Locale.current
formater.numberStyle = .decimal
return formater
}
And when you are setting the result to the label, you can go:
result.text = formater.string(from: NSDecimalNumber(value: yourValue))
If you are making a calculator I would recommend you that for bigger numbers or for numbers with many decimal places, you set the numberStyle property to .scientific.

How to add two numbers in Swift only when both of the text fields are filled in

I'm trying to add two numbers in Swift 5, and I want to add some error checks. I don't want to make it possible for a user to click on the plus button if both of the text fields are not filled in. I tried with the if state below but it did not work.
the whole function:
#IBAction func sum(_ sender: Any) {
let one = input1.text
let oneInt = Int(one!)
let two = input2.text
let twoInt = Int(two!)
let total = oneInt! + twoInt!
label.text = "\(total)"
if(input2.text == nil){
addBtn.isEnabled = false
}
if(input1.text == nil){
addBtn.isEnabled = false
}
}
Try to use guard like this. If your input field does not contain any value that field return blank string and when you try to get integer value from that string it will return nil and your add button will be disable.
#IBAction func sum(_ sender: Any) {
guard let text1 = input1.text, let intValue1 = Int(text1) else {
addBtn.isEnabled = false
return
}
guard let text2 = input2.text, let intValue2 = Int(text2) else {
addBtn.isEnabled = false
return
}
label.text = "\(intValue1 + intValue2)"
}
A nice and simple way is to addTarget to your textFiels. This will enable you to handle the events on the text field. In this scenario we'll use .editingChanged and use a single selector to achieve our goal:
What we'll do : We will listen for when someone types something in the textfield. Whenever a text changed was made, we'll check to see if all the textfields was populated and then if it was we enable the sum button.
A small controller sample :: Make sure to read the comments to understand the code faster
import UIKit
class ViewController: UIViewController {
#IBOutlet var textfield1: UITextField!
#IBOutlet var textfield2: UITextField!
#IBOutlet var sumButton: UIButton!
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
sumButton.isEnabled = false /// Disable the button first thing
[textfield1, textfield2].forEach {
$0.addTarget(self, action: #selector(editingChanged(_:)), for: .editingChanged) /// add targets to handle the events (in your case it listens for the 'editingChanged' event )
}
}
#objc func editingChanged(_ textField: UITextField) {
/// Here we just loop through all our textfields
for each in [textfield1, textfield2] {
if let text = each?.text { /// Just make sure the textfields text is not nil
if text.count < 1 {
// If the textfiels text has no value in, then we keep the button disabled and return
sumButton.isEnabled = false
return
}
} else {
/// Else if the text field's text is nill, then return and keep the button disabled
sumButton.isEnabled = false
return
}
}
sumButton.isEnabled = true /// If the code reaches this point, it means the textfields passed all out checks and the button can be enabled
}
#IBAction func sum(_ sender: Any) {
let one = textfield1.text!
let two = textfield2.text!
guard let oneInt = Int(one), let twoInt = Int(two) else {
print("Whatever was in that text fields, couldn't be converted to an Int")
label.text = "Be sure to add numbers."
return
}
let total = oneInt + twoInt
label.text = "\(total)"
}
}
textfields are not nil but empty strings. so make your comparison like :
if input1.text == "" {
// do your check here
}
Seems like you want to start with addBtn.isEnabled = false then update it whenever the user enters two valid integers into the text fields, i.e. Int(input1.text ?? "") != nil && Int(input2.text ?? "") != nil. You can do this by adding a target to your textfields (input1 and input2) for .editingChanged events. For example, if you're doing this in a UIViewController, you can do this in viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
addBtn.isEnabled = false
input1.addTarget(self, action: #selector(textFieldDidEdit(_:)), for: .editingChanged)
input2.addTarget(self, action: #selector(textFieldDidEdit(_:)), for: .editingChanged)
}
Where textFieldDidEdit(_:) action looks like:
#objc func textFieldDidEdit(_ sender: UITextField) {
addBtn.isEnabled = Int(input1.text ?? "") != nil && Int(input2.text ?? "") != nil
}
Finally your sum function becomes:
#IBAction func sum(_ sender: UIButton) {
guard let oneInt = Int(input1.text ?? ""), let twoInt = Int(input2.text ?? "") else {
return
}
let total = oneInt + twoInt
label.text = "\(total)"
}
As all of the number validation has moved to the textFieldDidEdit(_:) function.

Adding Double and a String to then display on a UILabel

I am trying to add up a value that is entered in the text field with a value specified as a double and then returning the value on a label. The code that I have is :
#IBOutlet weak var enterField: UITextField!
var weekOneTotal:Double = 0
#IBAction func addButton(_ sender: Any) {
addCorrectValue()
}
func addCorrectValue () {
guard let addAmount = convertAmount(input: enterField.text!) else {
print("Invalid amount")
return
}
let newValue = weekOneTotal += addAmount
secondScreen.weekOneAmountLabel.text = String(newValue)
}
func convertAmount (input:String) -> Double? {
let numberFormatter = NumberFormatter ()
numberFormatter.numberStyle = .decimal
return numberFormatter.number(from: input)?.doubleValue
}
Try this:
func addCorrectValue () {
guard let addAmount = Double(enterField.text!) else {
print("Invalid amount")
return
}
let newValue = weekOneTotal + addAmount
secondScreen.weekOneAmountLabel.text = "\(String(format: "%.1f", newValue))"
}
The .1 is the number of decimals that are shown. You can adjust that to your needs. Hope I understood the question and this works for you!
You probably want to increase value of weekOneTotal variable by converted amount and then you want to use this value as text of some label
weekOneTotal += addAmount
secondScreen.weekOneAmountLabel.text = String(weekOneTotal)

Empty array causing my app to crash in Swift

I keep coming across this error "fatal error: Index out of range", after researching this I am still unsure of exactly how to fix this issue. To give context I have started off with an empty array var playersArray = [UITextField]() so that users can enter their names in order to play the game. I then make sure that the user has or has not entered a value into the text field for each name slot
if let player1Name = name1.text, !player1Name.isEmpty
{ playersArray.append(name1)
} else {
print("Player 1 Empty")
If the player has entered a value into that textfield then ill append that value to the array.
The issue I have is that if I run the game and no user has entered a name into any of the 10 textfields then the game will crash. Im assuming this is because the array is empty?
The error appears on this line where I randomize the names used for the game with the number of elements in the array:
let RandomPlayer = playersArray[Int(arc4random_uniform(UInt32(playersArray.count)))]
I assume if the array is empty then .count will not work?
How can I make sure the game wont crash if the array is empty?
CODE:
var playersArray = [UITextField]()
override func viewDidLoad() {
super.viewDidLoad()
textColor()
question1View.isHidden = true
questionLabel.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// Alert message on startup
func alertMessageOnStartUp(){
let alert = UIAlertController(title: "Warning!", message: "Please drink responsibly. By continuing, you agree that you are responsible for any consequences that may result from BottomsUp.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Agree", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
// Dismiss keyboard when tapped outside the keyboard
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
// Dimiss keybaord when return button is tapped
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
name1.resignFirstResponder()
name2.resignFirstResponder()
name3.resignFirstResponder()
name4.resignFirstResponder()
name5.resignFirstResponder()
name6.resignFirstResponder()
name7.resignFirstResponder()
name8.resignFirstResponder()
name9.resignFirstResponder()
name10.resignFirstResponder()
return(true)
}
//randomise background colour of each question page
func getRandomBackgroundColor() -> UIColor{
let randomRed:CGFloat = CGFloat(drand48())
let randomGreen:CGFloat = CGFloat(drand48())
let randomBlue:CGFloat = CGFloat(drand48())
return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0)
}
func textColor(){
name1.textColor = UIColor.white
name2.textColor = UIColor.white
name3.textColor = UIColor.white
name4.textColor = UIColor.white
name5.textColor = UIColor.white
name6.textColor = UIColor.white
name7.textColor = UIColor.white
name8.textColor = UIColor.white
name9.textColor = UIColor.white
name10.textColor = UIColor.white
}
#IBAction func playButton(_ sender: Any) {
alertMessageOnStartUp()
if let player1Name = name1.text, !player1Name.isEmpty
{ playersArray.append(name1)
} else {
print("Player 1 Empty")
}
if let player2Name = name2.text, !player2Name.isEmpty
{ playersArray.append(name2)
} else {
print("Player 2 Empty")
}
if let player3Name = name3.text, !player3Name.isEmpty
{ playersArray.append(name3)
} else {
print("Player 3 Empty")
}
if let player4Name = name4.text, !player4Name.isEmpty
{ playersArray.append(name4)
} else {
print("Player 4 Empty")
}
if let player5Name = name5.text, !player5Name.isEmpty
{ playersArray.append(name5)
} else {
print("Player 5 Empty")
}
if let player6Name = name6.text, !player6Name.isEmpty
{ playersArray.append(name6)
} else {
print("Player 6 Empty")
}
if let player7Name = name7.text, !player7Name.isEmpty
{ playersArray.append(name7)
} else {
print("Player 7 Empty")
}
if let player8Name = name8.text, !player8Name.isEmpty
{ playersArray.append(name8)
} else {
print("Player 8 Empty")
}
if let player9Name = name9.text, !player9Name.isEmpty
{ playersArray.append(name9)
} else {
print("Player 9 Empty")
}
if let player10Name = name10.text, !player10Name.isEmpty
{ playersArray.append(name10)
} else {
print("Player 10 Empty")
}
question1View.isHidden = false
question1View.backgroundColor = getRandomBackgroundColor()
let RandomPlayer = playersArray[Int(arc4random_uniform(UInt32(playersArray.count)))]
let RandomQuestion = questionArray[Int(arc4random_uniform(UInt32(questionArray.count)))]
questionLabel.text = RandomPlayer.text! + RandomQuestion
}
#IBAction func nextQuestionButton(_ sender: Any) {
question1View.backgroundColor = getRandomBackgroundColor()
let RandomPlayer = playersArray[Int(arc4random_uniform(UInt32(playersArray.count)))]
let RandomQuestion = questionArray[Int(arc4random_uniform(UInt32(questionArray.count)))]
questionLabel.text = RandomPlayer.text! + RandomQuestion
}
}
Breaking this down:
Int(arc4random_uniform(UInt32(playersArray.count)))
This line gets a random number with a minimum value of 0 and a maximum value of the length of the playersArray minus 1.
I'm actually not sure what it does when the argument you pass in is 0, but it doesn't really matter, as we'll see next.
Then you use that random value here:
playersArray[thatRandomNumber]
Because there are no elements in playersArray, no matter what the value is of thatRandomNumber, it's going to be out of bounds.
You probably want something more like this:
let RandomPlayer = <some default value>
if !playersArray.isEmpty {
RandomPlayer = playersArray[Int(arc4random_uniform(UInt32(playersArray.count)))]
}
EDIT
Your latest code still doesn't seem to do anything to prevent indexing into the empty array.
You have:
#IBAction func playButton(_ sender: Any) {
...
let RandomPlayer = playersArray[Int(arc4random_uniform(UInt32(playersArray.count)))]
let RandomQuestion = questionArray[Int(arc4random_uniform(UInt32(questionArray.count)))]
questionLabel.text = RandomPlayer.text! + RandomQuestion
}
You need:
#IBAction func playButton(_ sender: Any) {
...
if playersArray.isEmpty {
// do something about that
} else {
let RandomPlayer = playersArray[Int(arc4random_uniform(UInt32(playersArray.count)))]
let RandomQuestion = questionArray[Int(arc4random_uniform(UInt32(questionArray.count)))]
questionLabel.text = RandomPlayer.text! + RandomQuestion
}
}
playersArray.count for an empty array is 0, so you are trying to access playersArray[0] - but the array is empty, so nothing exists at the 0 index.
You should do something like this:
let randomPlayer: Player
if !playersArray.isEmpty {
randomPlayer = playersArray[Int(arc4random_uniform(UInt32(playersArray.count)))]
} else {
randomPlayer = Player() //create a fallback player
}
Alternatively you could make randomPlayer an optional, rather than providing a fallback value. Depends on your needs for that variable.

how to ignore a value of a UITextField that has already been calculated in swift 3?

I'm trying to make an app that is very basic. One part of the app is that there are 4 textFields and a button that calculates the sum of these textFields.
The problem that I'm facing is that say I type the value 10 in the first textField then I press the button. The result would be 10. However, if I press it again ( without typing anything in the other textFields), the result would be 20!! Furthermore, if I type 20 in one of the other textFields, the result would be 40!!
The result SHOULD BE 30 NOT 40!!
one possible option I thought of (haven't tried it yet) is assigning 0 to all of the textFields when pressing the button. But I'd like the app to be smarter and keep tracks of the result.
if it helps, here's the code inside the button that calculates the sum:
#IBAction func calBtnPressed(_ sender: UIButton) {
var benifit:[Double] = []
var textFields: [Double] = []
if initialBalance.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = initialBalance.text {
// these lines of code will convert arabic numbers to English ones in case the user uses Arabic number
let initialStr: String = temp
let initialFormatter: NumberFormatter = NumberFormatter()
initialFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let initialFinal = initialFormatter.number(from: initialStr)
benifit.append(Double(initialFinal!))
}
}
if income.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = income.text {
// these lines of code will convert Arabic numbers to English ones in case the user uses Arabic number
let incomeStr: String = temp
let incomeFormatter: NumberFormatter = NumberFormatter()
incomeFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let incomeFinal = incomeFormatter.number(from: incomeStr)
benifit.append(Double(incomeFinal!))
}
}
if salaries.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = salaries.text {
let salariesStr: String = temp
let salariesFormatter: NumberFormatter = NumberFormatter()
salariesFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let salariesFinal = salariesFormatter.number(from: salariesStr)
textFields.append(Double(salariesFinal!))
}
}
if tools.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = tools.text {
let toolsStr: String = temp
let toolsFormatter: NumberFormatter = NumberFormatter()
toolsFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let toolsFinal = toolsFormatter.number(from: toolsStr)
textFields.append(Double(toolsFinal!))
}
}
if maintinance.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = maintinance.text {
let maintinanceStr: String = temp
let maintinanceFormatter: NumberFormatter = NumberFormatter()
maintinanceFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let maintinanceFinal = maintinanceFormatter.number(from: maintinanceStr)
textFields.append(Double(maintinanceFinal!))
}
}
if other.text?.isEmpty ?? true {
// do nothing
} else {
if let temp = other.text {
let otherStr: String = temp
let otherFormatter: NumberFormatter = NumberFormatter()
otherFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let otherFinal = otherFormatter.number(from: otherStr)
textFields.append(Double(otherFinal!))
}
}
for textField in textFields {
sumExpenses += textField
}
for ben in benifit{
sumBenifit += ben
}
totalExpenses.text = String(sumExpenses)
totalAfterSubtractingExpenses.text = String( sumBenifit - sumExpenses )
sumBenifit -= sumExpenses
}
I think I found your problem.
You use a variable sumBenefit which isn't declared in your func, so I assume it is declared in your UIViewController.
Since it is an instance variable, it will not reset each time you click the button.
If you want to reset the values of sumExpenses and sumBenefits each time the button is pressed, then you'll have to do something like this:
sumExpenses = 0
for textField in textFields {
sumExpenses = Int(textField.text)!
}
sumBenefit = 0
for ben in benefit {
sumBenefit += ben
}
I am also making the assumption that you want a number from your textField in the first for-loop, because if sumExpenses is of type Int (or any other number for that matter) then sumExpenses += textField will not compile. You need to take the text of that textField and convert it to an Int.
Again, I am still not super clear what you are trying to do, but please let me know if this works for you, or if you need further clarification.

Resources