Rawkfist1501 Rawkfist1501 - 3 years ago 137
iOS Question

Swift: Need to move data stored using a button to another View Controller

Using a button to collect a phone number and then reading and storing the phone number under the button function.

@IBAction func viewRewardsButtonTapped(_ sender: UIButton) {
// Validate format
let phoneNumberInput = phoneInput.text

Is there a better way to store the phone number
and get it to another

I currently can't get the other
to recognize the variables stored under the

Answer Source

You could use UserDefaults:

// Set phone number
UserDefaults.standard.set(phoneInput,text, forKey: "phoneNumber")

// Get phone number
UserDefaults.standard.string(forKey: "phoneNumber")

Or if you want to segue at the same time that you´re clicking the button use this:

self.performSegue(withIdentifier: "showVC2", sender: phoneNumber.text)

override func prepare(for segue: UIStoryboardSegue, sender: Any!) {
    if (segue.identifier == "showVC2") {
        if let phoneNumber = sender as? String {
            let vc = segue.destination as! YourSecondVC
            vc.phoneNumber = phoneNumber

So you basically create the segue and declare a variable for phoneNumber in your second VC so that you can pass the value to it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download