Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send data back by popViewControllerAnimated for Swift?

Tags:

ios

swift

I need to send some data back from secondView to First View by popView. How can i send back the data by popViewControllerAnimated?

Thanks!

like image 356
Varis Darasirikul Avatar asked Jun 03 '15 10:06

Varis Darasirikul


People also ask

How to send notifications to multiple view controllers in Swift?

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.

What is a programmatic navigation controller 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.

How do I create a single view project in Swift?

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.

How do I pass data between view controllers?

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:


1 Answers

You can pass data back using delegate

  1. Create protocol in ChildViewController
  2. Create delegate variable in ChildViewController
  3. Extend ChildViewController protocol in MainViewController
  4. Give reference to ChildViewController of MainViewController when navigate
  5. Define delegate Method in MainViewController
  6. Then you can call 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) 
like image 134
iDhaval Avatar answered Sep 22 '22 01:09

iDhaval