I have parsed data from JSON link in a data source file how can I use that data to display information in the main view controller.
Asked
Active
Viewed 1,082 times
-1
-
Please add more information. where you are getting data? does you want to show in previous controller of navigation stack or on the same controller? – Mahak Mittal Feb 12 '19 at 10:16
-
Using delegate method how can I access the data from the data source file. Access data of one class into another class. From JSON link I am parsing data and want to display the data in the from another class – Nikhilesh Anand Feb 12 '19 at 10:16
-
Possible duplicate/related: https://stackoverflow.com/questions/24222640/passing-data-between-view-controllers-in-swift – Scriptable Feb 12 '19 at 10:17
-
I have added an answer how delegate works. Hope this helps – Mahak Mittal Feb 12 '19 at 10:26
-
When asking a question, you should show us some code of what you've tried and enter far more details of exactly what you're trying to do. That means that the community can give you a more precise answer rather than having to make some assumptions on the detail and possibly give answers that are not relevant to you. Here is a link to some help on how to ask a good question: https://stackoverflow.com/help/how-to-ask – Swinny89 Feb 12 '19 at 10:45
2 Answers
1
//Create protocol in second class from where you want to send data to previous one
protocol SampleDelegate: class {
func getData(data: YourDataType)
}
//Your sample class/ViewController
class SampleViewController: UIViewController {
// MARK:- Delegate
weak var delegate: SampleDelegate?
//From where you want to send data
delegate?.getData(data: Data)
}
//Main Class/ViewController
let detailViewController = self.storyboard?.instantiateViewController(withIdentifier: "SampleViewController") as! SampleViewController
detailViewController.delegate = self
self.navigationController?.pushViewController(detailViewController, animated: true)
func getData(data: YourDataType){
//This function get called when you call this method from Smapleview controller
}

AtulParmar
- 4,358
- 1
- 24
- 45

Mahak Mittal
- 121
- 1
- 10
1
I assume that,you want to pass data from VCB To VCA
IMPLEMENT DELEGATION
Create a protocol at the very top of VCB.
protocol VCBDelegate {
func passingName(string: String)
}
Now inside of our VCB class, we need to declare a delegate variable.
var delegate: VCBDelegate?
Then inside of btnPassDataPressed function, add the following code:
delegate?. passingName(string: "Sent from VCFinal")
adopt our VCBDelegate protocol. In VCA and make delegate self in viewDidLoad
class VCA: UIViewController, VCFinalDelegate
Go ahead and add the passingName function inside of VCA.
func passingName(string: String) {
print("Notified")
}