Swift Send Data to a ViewController from a ViewController [duplicate]

Let’s first get in one page. you have viewController and you are presenting another viewController from code? and you want to pass data of first viewController to second one?

In your case for data passing there is no need of “user defaults” and “CoreData”.

Since you are not using segues, You can simply do it by delegation or closure or using observers.

I am explaining delegation here.

for example

Protocol DataPasser {
func dataHolder(data: DataType)
}

then use delegate in your first viewController like

above ViewDidLoad() method

var delegate: DataPasser?

then when redirecting next viewController just call delegate?.dataHolder(yourData)

and in next viewController make this delegate self and make extension to use this delegate like extension SecondViewController: DataPasser and use delegate method and you will have your data in delegate method argument.

Hope this will help you. if not ask me again.. here to help.

Leave a Comment