Problems with my first app

Good morning guys, I'm a new user of this forum and a junior swift student....

I'll directly expose my troubles...

Building my first app I'd like to have a textField in which the user will put Double numbers, and a button named "addFounds", that put the inserted numbers inside a label built for the purpose.

Apparently, the building procedure went well, I had no errors by the compiler but, when I tried to use the app launching the simulator, I've seen that I built an app that bring the input number and put it in the label, since now that all ok, when I clear the input textField using a button built to do it, and I proceed to insert the second number and press the add button, the app changes the value in the label with the new number instead of sum it to the old one!
I hope that I was good explaining my little problem, I'll attach the source code below....

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var whiteField: UITextField!
    @IBOutlet weak var whiteTotal: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    
    @IBAction func addFounds(_ sender: Any) {          
        
        if let white = Double(whiteField.text!) {        
            
            var whiteResult:Double = 0
            whiteResult = whiteResult + white
            whiteTotal.text = String(whiteResult)
            
        }
    
    @IBAction func clearFieds(_ sender: Any) {
        
        whiteField.text = ""
    }
    
    @IBAction func clearTotals(_ sender: Any) {
        
        whiteTotal.text = "0"
    }
}

Thank you for the help and excuse me for the silly answer!!!

This is your problem. whiteResult is just 0 + white (so... the same as just saying โ€˜whiteโ€™).

I would suggest adding a Double variable in the view controller (as you did for the IBOutlets) to store the total. Then you add โ€˜whiteโ€™ to it each time and update the text field.

Hope that helps.

1 Like

@Karl, thank you for the help!!!

I'll try to apply your suggest and let you know if it works!

I would suggest adding a Double variable in the view controller (as you did for the IBOutlets) to store the total. Then you add โ€˜whiteโ€™ to it each time and update the text field.

I've tried to do it, surely in the wrong way, but I've just add another var that shows the same value and is overrode every time I give a new input value....

This is the code....

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var whiteField: UITextField!
    @IBOutlet weak var partialWhiteTotal: UILabel!
    @IBOutlet weak var whiteTotal: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    
    @IBAction func addFounds(_ sender: Any) {
        
        
        
        if let white = Double(whiteField.text!) {
    
            var whiteResult:Double = 0
            whiteResult += white
            partialWhiteTotal.text = String(whiteResult)   
        }
        whiteTotal.text = partialWhiteTotal.text

    @IBAction func clearFieds(_ sender: Any) {
        
        whiteField.text = ""
    }
    
    @IBAction func clearTotals(_ sender: Any) {
        
        whiteTotal.text = "0"
    }

:tired_face::tired_face::tired_face:

I think that I need to put inside the code a kind of loop but I'm not finding the right one!

I mean something like this:

class ViewController: UIViewController {
    
    @IBOutlet weak var whiteField: UITextField!
    @IBOutlet weak var whiteTotal: UILabel!

    // Instance variable to hold the running total.
    private var total: Double = 0
    
    // ...
    
    @IBAction func addFounds(_ sender: Any) {          
        
        if let white = Double(whiteField.text!) {        
            // Increment the running total.
            self.total += white
            // Update the 'whiteTotal' label.
            whiteTotal.text = String(self.total)          
        }
    
    // ...
}

Additionally, if you're having trouble with concepts like instance variables (I don't know if you have any prior programming experience), I recommend reading the Swift language guide.

Hope it helps.

1 Like

Thanks a lot @Karl!!!

I have some basic experience in Python and I attended a basic Swift4 programming course.

I like a lot coding so I'll surely study the guide you've indicated to me!!!!

I'll let you to know about my app!!! :wink:

1 Like

Hey @Karl, with your suggestions the app works perfectly!

Before I missed up the concept of private and instance var...

Thanks a lot again!!! :wink:

1 Like
Terms of Service

Privacy Policy

Cookie Policy