Different ways to pass data between viewcontrollers/views

  1. using property
  2. using functions.
  3. using init method
  4. using segues
  5. using closures
  6. using delegates
  7. using NotificationCenter
  8. using Singleton

Passing Data by using property or function:

1. Passing data between two viewcontroller using property 2. Passing data between two viewcontroller using function

Passing data in init method:

Passing data using segues:

  • You need to create a segue between viewcontrollers in storyboard
  • Assign an identifier name to segue.
  • use prepare(for:sender:) method to pass the function.
  1. Control + click the UI element you are going to use to make the bridge and drag to the second View Controller and then add the identifier to the segue.
1. Creating segue between VC by Control + click the UIButton 2. Creating Segue Identifier
  • after adding segue, add UIButton/UIControl subclass programatically or through storyboard
  • add Target/IBAction to the UIButton/UIControl.
  • then use performSegue(withIdentifier: “”, sender: ) in Target/IBAction to create the bridge between two view controllers.
1. Creating segue between viewcontrollers. create bridge by Control + click on viewcontroller 2. perform segue for IBAction with id and pass data to the secondVC from firstVC.
  • After creating segue using any one of the way, override the prepare(for segue: UIStoryboardSegue, sender: Any?) method to pass data.

Passing data with closures:

Passing data with delegates:

1. Implemented child VC delegate in parent VC 2. sending data from child VC back to it’s owner

Passing data by using Singleton object:

Singleton object is created and used in class A

Passing data by using Notification Center:

1. Sending data from second view controller using notification center post method. 2. Added observer in receiver view controller




iOS Developer in walmart

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Editing items in CoreData

SwiftUI Tutorial: Working with Picker

SwiftGen: The Swift Code Generator

SwiftUI Tutorial: Working with Form

Navigation in Swift UI

SwiftUI 2 | Play Video With AVPlayer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Manasa M P

Manasa M P

iOS Developer in walmart

More from Medium

Making a Cocoapod in Swift with an Objective-C .framework dependency

Finding Favicon’s — A brief explanation

Stretchy Header Collection View with images slider

So you want to do mock testing in XCTest?