Calculator button help

im trying to create a simple calculator where i want one button to give out 4 different answers in the 4 different label fields.


i have setup the fields and button but im unsure how to calculate to show in each box.



im trying to take 2 label boxes and calculate out into 4 boxes where each box is 25 percent,50 percent,75 percent and 100 percent.

Accepted Reply

So, with


value A = 10

value B = 20


then Result100 = 10 / 20 / 1.73 = 0.28


And you want

Result25 = 0.07

Result50 = 0.14

Result75 = 0.21

Result100 = 0.28


Is it correct ?


If so, calculate function is :


@IBOutlet weak var valueAField: UITextField!
@IBOutlet weak var valueBField: UITextField!

    @IBAction func calculate(sender: UIButton) {

          guard let valueAText = valueAField.text, let valueBText = valueBField.text, let valueA = Float(valueAText) , let valueB = Float(valueBText) else { return }
          if valueB < 0.00000001 && valueB > - 0.00000001     { return } // Avoid div by zero
          let computedValue = (valueA / valueB) / 1.73
          let value25 = 0.25 * computedValue
          let value50 = 0.5 * computedValue
          let value75 =  0.75 * computedValue
          label25.text = String(value25)
          label50.text = String(value50)
          label75.text = String(value75)
          label100.text = String(computedValue)
     }

If you want to have a single label for the result, you can ;


    @IBOutlet weak var result: UILabel!

     @IBOutlet weak var valueAField: UITextField!
     @IBOutlet weak var valueBField: UITextField!

    @IBAction func calculate(sender: UIButton) {

          guard let valueAText = valueAField.text, let valueBText = valueBField.text, let valueA = Float(valueAText) , let valueB = Float(valueBText) else { return }
          if valueB < 0.00000001 && valueB > - 0.00000001     { return } // Avoid div by zero
          let computedValue = (valueA / valueB) / 1.73
          let value25 = 0.25 * computedValue
          let value50 = 0.5 * computedValue
          let value75 =  0.75 * computedValue
          result.text = String(value25) + "   " +  String(value50) + "   " +  String(value75)  + "   " + String(computedValue)
  }

you can also limit to 3 digits after dot

replace

String(value25)

by

String(format: "%.3f",value25)

Replies

You have 4 TextFields, defined with their IBOutlets

@IBOutlet weak var label25: UILabel!
@IBOutlet weak var label50: UILabel!
@IBOutlet weak var label75: UILabel!
@IBOutlet weak var label100: UILabel!

and a textField to enter the value

@IBOutlet weak var inputField: UITextField!


In the IBAction for the button:


     @IBAction func calculate(sender: UIButton) {
         
          guard let initialValue = inputField.text, let value = Float(initialValue) else { return }
          let value25 = 0.25 * value
          let value50 = 0.5 * value
          let value75 = 0.75 * value
          label25.text = String(value25)
          label50.text = String(value50)
          label75.text = String(value75)
          label100.text = String(value)
     }




When you say:

im trying to take 2 label boxes and calculate out into 4 boxes where each box is 25 percent,50 percent,75 percent and 100 percent.


Do you mean you want to calculate the values between the 2 at 25, 50, 75% ?


If so,

You have 2 values : startValue and endValue

@IBOutlet weak var startValueField: UITextField!
@IBOutlet weak var endValueField: UITextField!

change the IBAction

    @IBAction func calculate(sender: UIButton) {

          guard let startValueText = startValueField.text, let endValueText = endValueField.text, let startValue = Float(startValueText) , let endValue = Float(endValueText) else { return }
          let spread = endValue - startValue
          let value25 = startValue + 0.25 * spread
          let value50 = startValue + 0.5 * spread
          let value75 = startValue + 0.75 * spread
          label25.text = String(value25)
          label50.text = String(value50)
          label75.text = String(value75)
          label100.text = String(endValue)
     }


If you want to add some fun, define the color of text of startValueField as Green ; and color of endValueField as red.

Then, show a progression of colors in the labels you write :


    @IBAction func calculate(sender: UIButton) {
       
        guard let startValueText = startValueField.text, let endValueText = endValueField.text, let startValue = Float(startValueText) , let endValue = Float(endValueText) else { return }
        var startRed: CGFloat = 0, startGreen: CGFloat = 0, startBlue: CGFloat = 0, startAlpha: CGFloat = 0
        var endRed: CGFloat = 0, endGreen: CGFloat = 0, endBlue: CGFloat = 0, endAlpha: CGFloat = 0

        let startColor = startValueField.textColor!
        let endColor = endValueField.textColor!
        startColor.getRed(&startRed, green: &startGreen, blue: &startBlue, alpha: &startAlpha)
        endColor.getRed(&endRed, green: &endGreen, blue: &endBlue, alpha: &endAlpha)

        let redSpread = endRed - startRed
        let greenSpread = endGreen - startGreen
        let blueSpread = endBlue - startBlue

        let spread = endValue - startValue
        let value25 = startValue + 0.25 * spread
        let value50 = startValue + 0.5 * spread
        let value75 = startValue + 0.75 * spread
       
        var colorRed = startRed + 0.25 * redSpread
        var colorGreen = startGreen + 0.25 * greenSpread
        var colorBlue = startBlue + 0.25 * blueSpread
        var colorLabel = UIColor(red: colorRed, green: colorGreen, blue: colorBlue, alpha: 1.0)
        label25.text = String(value25)
        label25.textColor = colorLabel
       
        colorRed = startRed + 0.5 * redSpread
        colorGreen = startGreen + 0.5 * greenSpread
        colorBlue = startBlue + 0.5 * blueSpread
        colorLabel = UIColor(red: colorRed, green: colorGreen, blue: colorBlue, alpha: 1.0)
        label50.text = String(value50)
        label50.textColor = colorLabel
       
        colorRed = startRed + 0.75 * redSpread
        colorGreen = startGreen + 0.75 * greenSpread
        colorBlue = startBlue + 0.75 * blueSpread
        colorLabel = UIColor(red: colorRed, green: colorGreen, blue: colorBlue, alpha: 1.0)
        label75.text = String(value75)
        label75.textColor = colorLabel
       
        label100.text = String(endValue)
        label100.textColor = endValueField.textColor!
    }

i dont believe i have enough boxes for what all im wanting but i will add more.


i want to calculate the values between value A and value B. with the answer each being 25,50,75,100 percent.using this equation.


valueA / ValueB / 1.73 = 100 percent.


then i want to devide the 100 percent into the 25,50,75 boxes


does this make since?






here is my current setup below

import UIKit


class ViewController: UIViewController {



@IBOutlet weak var ValueB: UITextField!

@IBOutlet weak var ValueA: UITextField!

@IBOutlet weak var Result25: UILabel!

@IBOutlet weak var Result50: UILabel!

@IBOutlet weak var Result75: UILabel!

@IBOutlet weak var Result100: UILabel!

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

}


override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

Not totally clear. What is this 1.73 ?

Do you set value A and value B or only value A and compute value B from value A ?


Let us take an example:


value A = 10

value B = 20 // Or is it computed as value A / 1.73 ?


What do you want to get in

@IBOutlet weak var Result25: UILabel!

@IBOutlet weak var Result50: UILabel!

@IBOutlet weak var Result75: UILabel!

@IBOutlet weak var Result100: UILabel!


I thought it was 12.5 15 17.5 20

the valueA and valueB are both user inputs where


valueA divided by ValueB divided by 1.73 is equal to Result100


but then i want the same button to divid Result100 into the other 25,50,75 boxes.


sorry for being such a noob! but thanks for heading me in the right direction!

So, with


value A = 10

value B = 20


then Result100 = 10 / 20 / 1.73 = 0.28


And you want

Result25 = 0.07

Result50 = 0.14

Result75 = 0.21

Result100 = 0.28


Is it correct ?


If so, calculate function is :


@IBOutlet weak var valueAField: UITextField!
@IBOutlet weak var valueBField: UITextField!

    @IBAction func calculate(sender: UIButton) {

          guard let valueAText = valueAField.text, let valueBText = valueBField.text, let valueA = Float(valueAText) , let valueB = Float(valueBText) else { return }
          if valueB < 0.00000001 && valueB > - 0.00000001     { return } // Avoid div by zero
          let computedValue = (valueA / valueB) / 1.73
          let value25 = 0.25 * computedValue
          let value50 = 0.5 * computedValue
          let value75 =  0.75 * computedValue
          label25.text = String(value25)
          label50.text = String(value50)
          label75.text = String(value75)
          label100.text = String(computedValue)
     }

If you want to have a single label for the result, you can ;


    @IBOutlet weak var result: UILabel!

     @IBOutlet weak var valueAField: UITextField!
     @IBOutlet weak var valueBField: UITextField!

    @IBAction func calculate(sender: UIButton) {

          guard let valueAText = valueAField.text, let valueBText = valueBField.text, let valueA = Float(valueAText) , let valueB = Float(valueBText) else { return }
          if valueB < 0.00000001 && valueB > - 0.00000001     { return } // Avoid div by zero
          let computedValue = (valueA / valueB) / 1.73
          let value25 = 0.25 * computedValue
          let value50 = 0.5 * computedValue
          let value75 =  0.75 * computedValue
          result.text = String(value25) + "   " +  String(value50) + "   " +  String(value75)  + "   " + String(computedValue)
  }

you can also limit to 3 digits after dot

replace

String(value25)

by

String(format: "%.3f",value25)

that seems close! and i think it might be right on target for what im looking for.


but when i try and run the similator it crashed out with a class AppDelegate: UIResponder, UIApplicationDelegate { Thread1:breakpoint 1.4

error.


i changed the 25,50,75 fields to line up with the naming of my board.

You have changed the names of the IBOutlets without removing the connections first.

So now connections are corrupted.


In Interface Builder, remove all those connections

- select the firld

- open the Connections inspector

- click the small x

- now, you should see in code that the circles next to IBOutlet declaration are no more black, but empty


Reconnect all the IBOutlets


Do a clean build forlder by security

i think im in over my head 😟

What is it you don't understand ?

How do i get to link the buttons again?in the interface builder?

To reconnect an IBOutlet (button or label or whatever), you have several ways:

- control drag from the small white dot on the left of IBOutlet declaration onto the object in IB (button or label or whatever) ; the white dot will become black

or

- control drag from the object in IB (button or label or whatever) onto IBOutlet declaration ; the IBOutlet will highlite ; then the white dot will become black


To connect a button to its IBAction, do the same, but the drag is toward the IBAction function.

So when i control+drag do i name the new just like the code suggest?

If you have already written the IBOutlet declaration that you want to connect to, you control-grag on it ; you will not be asked for a name.


If you want to create a new IBOutlet, then you have to give it a name in the black window.

sorry i havent had time to work on this. but im still struggling here on the linking buttons.


heres what i have so far


class ViewController: UIViewController {

@IBOutlet weak var result: UILabel!

@IBOutlet weak var valueAField: UITextField!

@IBOutlet weak var valueBField: UITextField!

@IBAction func calculate(sender: UIButton) {

guard let valueAText = valueAField.text, let valueBText =

valueBField.text, let valueA = Float(valueAText) , let valueB

= Float(valueBText) else { return }

if valueB < 0.00000001 && valueB > -0.00000001 { return } //Avoid div by zero

let computedValue = (valueA / valueB) / 1.73

let Result25 = 0.25 * computedValue

let Result50 = 0.5 * computedValue

let Result75 = 0.75 * computedValue

result.text = String(Result25) + " " + String(Result50) + " " + String(Result75) + " " + String(computedValue)

So, what is the problem ?

Which button do you have problem to link to its IBAction ?


Do you see black dot just on the left of @IBAction ?

If so, select the button in IB.

Open the connections panel on the right.

Do you see in front of Touch Up Inside, the name of the func calculate ?


If so, it is correctly linked.