Detect when a button is pressed outside of the IBAction in Swift? - ios

I'm a beginner programmer making my first real app, a calculator in Swift.
It's mostly working, but one issue I'm running into is how it displays numbers after pressing one of the operator buttons. Currently, whenever an operator button is pressed, I have it set the label at the top of the calculator to "0". But on actual calculators, this top display won't change until another number button is pressed.
If I don't reset the display to 0, then any number buttons that are pressed will add to the current text at the top, and mess up the equation that the calculator will have to do (i.e. 2+2 displays 22, and the solution it displays is 22+2=24)
I'm wondering if it's possible to detect when one of the number buttons is pressed (listed in my code as the intButtonPressed IBAction) outside of the intButtonPressed function? That way I can keep the top label the same until the user starts inputting more text, then I can set it to 0 to prevent the calculator from breaking.
Any other possible (better) solutions would be welcome as well
Here's my code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var topLabel: UILabel!
var num1 = Double()
var solution = Double()
var op = String()
#IBAction func intButtonPressed(_ sender: UIButton) {
if topLabel.text == "0" {
topLabel.text = sender.currentTitle
}
else if topLabel.text == String(solution) {
num1 = Double(topLabel.text!)!
solution = 0.00
topLabel.text = sender.currentTitle
// Basically stops user from adding to solution?
// Also stores solution as num1 and clears solution field
}
else {
topLabel.text = topLabel.text! + sender.currentTitle!
// Keep typing
}
if (topLabel.text?.count)! > 12 {
topLabel.text = "Error"
}
else {
return
}
// if its greater than 12 characters, display "error"
}
#IBAction func clearButtonPressed(_ sender: UIButton) {
num1 = 0.00
solution = 0.00
topLabel.text = "0"
}
#IBAction func opButtonPressed(_ sender: UIButton) {
if sender.currentTitle == "=" {
equals()
}
else {
op = sender.currentTitle!
num1 = Double(topLabel.text!)!
topLabel.text = "0"
}
// Need it to display num1 until I press another button, then I need it to only display that text.
}
func equals() {
switch op {
case "+":
add()
case "-":
subtract()
case "×":
multiply()
case "÷":
divide()
default:
print(Error.self)
}
}
func add() {
solution = num1 + Double(topLabel.text!)!
topLabel.text = String(solution)
}
func subtract() {
solution = num1 - Double(topLabel.text!)!
topLabel.text = String(solution)
}
func multiply() {
print("topLabel = ", topLabel.text!)
solution = num1 * Double(topLabel.text!)!
print("solution = ", solution)
topLabel.text = String(solution)
}
func divide() {
solution = num1 / Double(topLabel.text!)!
//answer()
}
}

Update for anyone who finds this in the future: I've solved the issue, and realized that I wasn't very clear with what I wanted it to do. I solved the problem simply by adding a condition to the if/else statement in the inButtonPressed function that detects if the topLabel is 0. By rewriting that to detect if the topLabel is 0 OR the same as num1, and then changing the else statement in the opButtonPressed function to set topLabel to String(num1), the program does exactly what I want. Here's the rewritten code:
#IBAction func intButtonPressed(_ sender: UIButton) {
if topLabel.text == "0" || topLabel.text == "0.0" || topLabel.text == String(num1){
dotPressed = false
// Resets dotPressed whenever the top is 0 or num1 and an int button is pressed
topLabel.text = sender.currentTitle
}
else if topLabel.text == String(solution) {
num1 = Double(topLabel.text!)!
solution = 0.00
topLabel.text = sender.currentTitle
// Basically stops user from adding to solution?
// Also stores solution as num1 and clears solution field
}
else {
topLabel.text = topLabel.text! + sender.currentTitle!
}
}
#IBAction func opButtonPressed(_ sender: UIButton) {
if sender.currentTitle == "=" {
equals()
}
else {
op = sender.currentTitle!
num1 = Double(topLabel.text!)!
topLabel.text = String(num1)
}
// Successfully displays num1 until I press another button, then only displays that text.
I also added a separate function to handle decimals, and I had to update the code in there as well to keep that working.

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.

How not to show 0´s in label?

I have some buttons that print some text in a label when pushed. I also have a counter in front of the text to show how many times the respective button is pushed. But if some of the buttons isn´t pushed, it shows 0 in the label. Is it possible to convert the initial value on the int to blank space?
I have tried something like this:
var addCount:int = 0
var blankSpace = ""
if addCount == 0 {
addcount = blankSpace
}
else {
....
}
I have also tried:
if addCount == 0 {
addCount = String (blankSpace)
}
else {
}
and I have tried:
if addCount == 0 {
addCount = String ("")
}
I´m probably going about this all wrong, and I would appreciate help with this.
*
*
*
Edit: I thought there was some magic trick in swift to make the 0 disappear, so I took some shortcuts when I asked my question. Here is the whole setup:
I have four buttons in View Controller 1 that prints some text and adds 1 to counter. The data is based through a Swift file allSum.
The label that displays the text and counter is in View Controller 2.
allSum:
import UIKit
class allSum: NSObject {
var btn1Pressed:String = ""
var addToBtn1:Int = 0
var btn2Pressed:String = ""
var addToBtn2:Int = 0
var btn3Pressed:String = ""
var addToBtn3:Int = 0
var btn4Pressed:String = ""
var addToBtn4:Int = 0
func printToLabel() -> String{ //return a string with the current status
return ("\(addToBtn1) \(btn1Pressed + "")") + ("\(addToBtn2) \(btn2Pressed + "")") + ("\(addToBtn3) \(btn3Pressed + "")") + ("\(addToBtn4) \(btn4Pressed + "")")
}
View Controller 1
var total = allSum
var button1 = "Button 1 Pressed\n"
var button2 = "Button 2 Pressed\n"
var button3 = "Button 3 Pressed\n"
var button4 = "Button 4 Pressed\n"
#IBAction func no1(_ sender: UIButton) {
total.btn1Pressed = button1
toal.addToBtn1 += 1
}
#IBAction func no2(_ sender: UIButton) {
total.btn2Pressed = button2
toal.addToBtn2 += 1
}
#IBAction func no3(_ sender: UIButton) {
total.btn3Pressed = button3
toal.addToBtn3 += 1
}
#IBAction func no4(_ sender: UIButton) {
total.btn4Pressed = button4
toal.addToBtn4 += 1
}
View Controller 2
var total = allSum?
label.text = total.printToLabel()
With this setup, the label is showing 0 0 0 0 when no buttons are pressed.
So, my problem is that I can't set the label.text = "" since I also have other variables printed to it.
Sorry for my initial laziness
Set the actual label text as a blank space.
var addCount:Int = 0
var blankSpace = ""
if addCount == 0 {
yourlabel.text = blankSpace
} else {
yourlabel.text = "\(addCount)"
}
UPDATED:
Here's a way to write your current code with less code and fix your issue of changing 0's to blank strings
AllSum
class AllSum {
var sumsArray: [Int] = [0,0,0,0]
func stringForLabel() -> String { //return a string with the current status
var string = ""
for (index, value) in self.sumsArray.enumerated() {
if value > 0 {
string = string + "\(value) Button \(index + 1) was pressed\n"
}
}
return string
}
}
View Controller 1
You can use tags in UIBuilder, to match the indices in your array. Link up all buttons to the same function
var total = AllSum()
#IBAction func numberPressed(_ sender: UIButton) {
total.sumsArray[sender.tag] = sumsArray[sender.tag] + 1
}
View Controller 2
var total = AllSum()
label.text = total.stringForLabel()
You won't be able to assign a string, i.e. "", to a variable you've declared as an Int. In this case you'd be better off dealing with this at the time that you set the text on the label. You can use a simple if/else statement, or a ternary operator if you want a one-liner:
if addCount == 0 {
self.label.text = ""
} else {
self.label.text = "\(addCount)"
}
And for the ternary option:
self.label.text = addCount == 0 ? "" : "\(addCount)"
You're trying to set an Int to a String. Swift is a type safe language and doesn't let you do that. If you need, for some reason, to have the same variable represent both states, you can use an enum with an associated value and still use it as long as it is Describable.
Instead you can just have a computed value that returns a String that returns a blank String if the variable is zero:
func displayCount () -> String {
return addCount == 0 ? String() : String(describing:addCount)
}

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

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
}

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP,subcode = 0x0)

class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
var userIsInTheMiddleOfTypingNumber: Bool = false
#IBAction func appendDigit(sender: UIButton) {
let digit = sender.currentTitle!
if (userIsInTheMiddleOfTypingNumber) {
label.text = label.text! + digit
}
else {
label.text = digit
userIsInTheMiddleOfTypingNumber = true
}
}
}
I am currently creating a calculator app. For some reason, when I run the simulator and try to input some digits in, the siumulator freezes, and the error of bad_instruction appears on the line:
label.text = digit
How would I fix this situation?
Looks like you forgot to connect the label in the Storyboard with the outlet.
Looks like the currentTitle property is nil which results in a crash. You are force unwrapping the property without any nil check.
#IBAction func appendDigit(sender: UIButton) {
if let digit = sender.currentTitle {
if (userIsInTheMiddleOfTypingNumber) {
label.text = label.text! + digit
}
else {
label.text = digit
userIsInTheMiddleOfTypingNumber = true
}
}
}
OR
It could be the reason specified by #dasdom

Resources