I need to send some data back from secondView to First View by popView. How can i send back the data by popViewControllerAnimated?
Thanks!
To handle the notification when it gets sent, you should implement a method and use it as the selector parameter for the addObserver implemented above: Finally, to send the notification from another View Controller, just use the post method with your notification name: So, these were five ways of passing data between View Controllers in Swift.
Swift Swift: Programmatic Navigation View Controllers in Swift. Navigation controllers are the workhorse of organizing view controllers. I’ve covered much of their use in other posts about MVC, segues and delegates. In this chapter, We’ll go through some of the Swift code for the Navigation controller.
Start a new single view project in Swift called SwiftProgNavControllerDemo . Go into the storyboard and select the blank view controller. Be sure to select the controller and not the view.
Photo by the author. In this article, we’re going to explore five ways to pass data between View Controllers, with Swift code snippets and examples. The five ways are: 1. Segues Segues are a storyboard mode to pass data. Imagine your app has an onboarding screen, and you want to ask the user's name to address them later on another screen:
You can pass data back using delegate
protocol in ChildViewController delegate variable in ChildViewController ChildViewController protocol in MainViewController ChildViewController of MainViewController when navigate delegate Method in MainViewController delegate method from ChildViewController Example
In ChildViewController: Write code below...
protocol ChildViewControllerDelegate { func childViewControllerResponse(parameter) } class ChildViewController:UIViewController { var delegate: ChildViewControllerDelegate? .... } In MainViewController
// extend `delegate` class MainViewController:UIViewController,ChildViewControllerDelegate { // Define Delegate Method func childViewControllerResponse(parameter) { .... // self.parameter = parameter } } There are two options:
A) with Segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { let goNext = segue.destinationViewController as ChildViewController goNext.delegate = self } B) without Segue
let goNext = storyboard?.instantiateViewControllerWithIdentifier("childView") as ChildViewController goNext.delegate = self self.navigationController?.pushViewController(goNext, animated: true) Method Call
self.delegate?.childViewControllerResponse(parameter)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With