-1

I have two view controllers and I want to pass the number from one view controller to the next using a button. But after I pass that number, I want that number to add to the next one so it keeps adding everytime the button is pressed. So far it works just by passing the string but I can't figure out how to convert that string to a number and then add that number. The string is stored in Firebase because I want to be able to change that number being added.

Here is my code:

View Controller with Button

@IBAction func verifyButton(_ sender: Any) {

    print("Button Clicked")

    let ref3 = Database.database().reference()
    ref3.child("Pay/Ad001").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapshot) in   

    })

    let myVC = storyboard?.instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileViewController      

    myVC.stringPassed = LabelOne.text!

    navigationController?.pushViewController(myVC, animated: true)
    UserDefaults.standard.setValue(LabelOne.text!, forKey: "stringPassed")
}

2nd View Controller with numbers I want being added:

override func viewDidLoad() {
    super.viewDidLoad()

    //pass number here and add
    totalEarned.text = stringPassed
    totalEarned!.text=UserDefaults.standard.string(forKey: "stringPassed")
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Carlos Alan
  • 27
  • 11
  • 4
    `UserDefaults` is the completely wrong way to pass data between view controllers. – vadian Aug 22 '17 at 19:18
  • Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Dávid Pásztor Aug 23 '17 at 16:18

1 Answers1

5

but I can't figure out how to convert that string to a number and then add that number

The entire question is a Bad Smell. You are violating MVC (model-view-controller).

Store and pass the number as a number. The number is model data; it needs to be a number, for the very reason that you will be adding something to it later.

If the number is to be displayed in the interface, the display is view and probably wants a string. So the view controller will convert it to a string only for purposes of display at the time that it tells the view what to display.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I did try and store it initially as a number instead of a string but then when I try to update the label it says it can't store a number it has to be a string. Thanks for your response Matt. – Carlos Alan Aug 22 '17 at 19:39
  • Correct, that's exactly what I said. The label does not "store" a number. It doesn't store anything. You should not be using a label as storage. The label _displays_. It wants a string to display, so you convert the number to a string just for purposes of setting the label's text. – matt Aug 22 '17 at 19:40
  • Thats what i'm trying to say. I have it as a number on the 1st view controller but how do I pass it to the second view controller as a number instead of passing it as a string? – Carlos Alan Aug 22 '17 at 19:45
  • You can pass the value as by coverting it like that strNumber.integerValue. in another vc assign it to your label as like label.text = string(int (label.text) + (numberPassedFromFirstVC)) – Amit Kumar Aug 22 '17 at 19:49
  • 1
    "I have it as a number on the 1st view controller but how do I pass it to the second view controller as a number instead of passing it as a string?" So this is a question about how to pass a value from one view controller to another? But that is one of the most commonly asked and answered questions on Stack Overflow. Search and ye shall find. – matt Aug 22 '17 at 20:21