DEV Community

Avelyn Hyunjeong Choi
Avelyn Hyunjeong Choi

Posted on

Data transfer between view controllers

main images

1.Use segue between button and thirdVC

  • check button is connected to thirdVC
  • this method is called automatically when we use segue to connect two view controllers
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        var result:String = ""
        guard let num = Int(numberTextField.text!) else {
            return
        }

        if num % 2 == 0 {
            result = "Even"
        } else {
            result = "Odd"
        }

        guard let vc = segue.destination as? thirdVC else {
            return
        }
        vc.result = result
    }
Enter fullscreen mode Exit fullscreen mode

2.Use segue between whole secondVC and thirdVC

  • click square on secondVC - connect to thirdVC
  • click segue and give identifier in the insepector area (navigateToThirdVC) under storyboard segue > identifier
  • this line will initiate relationship between secondVC and thirdVC
performSegue(withIdentifier: "navigateToThirdVC", sender: nil)
Enter fullscreen mode Exit fullscreen mode

option2

3.Give unique identifier to thirdVC

  • click thirdVC controller > identity inspector > Storyboard ID > resultVC
  • get a reference to the result screen (thirdVC)
  • this does not use segue at all! -> so don't need prepare() method
guard let resultScreen = storyboard?.instantiateViewController(withIdentifier: "resultVC") as? thirdVC else {
            print("can't find thirdVC")
            return
}
Enter fullscreen mode Exit fullscreen mode

option3

// pass the data to thirdVC
resultScreen.result = result

// Several options we can get to the thirdVC

// 1.navigate to result screen using parent contoller - navigationController
// this way, this page have back button!
self.navigationController?.pushViewController(resultScreen, animated: true)

// 2.we can also use modal presentation instead of navigationController
// this will pop up like a card! user can dismiss the card!
self.present(resultScreen, animated: true)

// 3.another option in modal presentation - card that user can't dismiss! (force)
resultScreen.modalPresentationStyle = .fullScreen
self.present(resultScreen, animated: true)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more