How to Pass Information Between Storyboard Segues

Passing data between View Controllers using Segue

"Passing data to the destination Controller" when a segue is triggered will be achieved by overriding method prepareForSegue:sender:.

Generally, you pass data and NOT the source view controller, to the destination view controller. "data" may be a certain aspect of your application "model". It's an object like "User", or perhaps an array containing "User", etc.

The destination view controller shall not have any knowledge of the source view controller. That means, the destination view controller does not need to import the header of the source view controller.

On the other hand, the source view controller may have knowledge of the concrete class of the destination view controller or a base class of the destination view controller, and thus will import the header of the destination view controller.

See: Configuring the Destination Controller When a Segue is Triggered

If you need some sort of "communication protocol" between the source and the destination, you might employ Delegation to communicate with other view controllers. This involves the definition of a @protocol (e.g. having a method doneButton) and a property delegate which is defined in the destination view controller. The protocol should be defined in the header of the destination view controller, if it's specific to the destination view controller. Usually, you define tho protocol from the view point of the destination controller, and not from the requirements of the source controller.

The source view controller then creates a delegate (unless, it itself is it already) and sets the delegate of of the destination view controller. The destination view controller will send the delegate methods to the delegate, and the delegate handles it.

Now, passing "data" from a VC_A to VC_B should be straight forward. You should read a few examples which use prepareForSegue:sender:. For example, the destination view controller may have a property data which represents the thing it should display. The source view controller must set this property in prepareForSegue:sender:.

Passing data from VC_A over VC_B to VC_C should be straight forward as well.

Note: Each view controller may tailor (separate, modify, prepare, slice, transform, etc.) its data to make it a suitable data for the next view controller.


If VC_C needs data that is not available in its source view controller VC_B, then there are a couple of approaches to solve this. However, this is usually a sign of bad design.

You could have an application model, which is global. Suppose, your "application model" is an object of type Document. Suppose, at any time there is only one instance of that application model. Then, the model is a "Singleton", which could be accessed from anywhere in your app like so:

Document* document = [Document sharedDocument];

However, the preferred way to get an instance of a model is in the first view controller which requires access to it, in this case: VC_A.

Then, VC_A passes a Document instance to the next view controller VC_B. And VC_B passes the document object to VC_C.

You should read the official documentation "View Controller Programming Guide for iOS".


Example 1

Suppose, you have a list "Users". The list should be displayed in a table view controller, and there should also be a detail view, showing details of one User.

The table view controller will have a "data" property users:

In UsersTableViewController.h:

@interface UsersTableViewController : UIViewController
@property (nonatomic, readonly) NSArray* users;
@end

(Strictly, this user property doesn't need to be public. For example, if the table view internally obtains the list of users itself, there is not need to access it from outside.

The "users" array is the data of the table view which shall be displayed in rows. Each row shows a "summary" of a user.

More details of a user should be displayed in a detail view controller. The detail view controller's data is a single user of type User.

When the user tabs a certain row in the table view, the detail view controller will be displayed. Before it will be displayed, the table view controller must configure the detail view controller: the table view controller assigns the detail view controller's "data property" the current selected user. Thus, the detail view controller should have a public property user:

@interface UserViewController : UIViewController
@property (nonatomic) User* user;
@end

The table view controller configures the detail view controller in prepareForSegue:sender::

In UsersTableViewController.m

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"ShowUserDetails"]) {
UserViewController* userViewController = [segue destinationViewController];
userViewController.user = [self.users objectInListAtIndex:[self.tableView indexPathForSelectedRow].row];
}
}

Example 2

The second example is more complex and uses "Delegation" as a means to establish a communication between controllers.

Caution:

This is not a complete example. The purpose of this example shall demonstrate how to use "Delegation". A full featured implementation of data tasks as shown in the example will require significant more effort. In this scenarios like these, "Delegation" will be the most preferred approach to accomplish this (IMHO).

Suppose we want to

  • Show a User
  • Modify (edit) a User
  • Create New a User, and
  • Delete a User

from within the detail view.

These "data tasks" shall not be performed by the detail view controller itself, instead a delegate is responsible for these data tasks.

These data actions shall be handled by the delegate:

@protocol UserDataSourceDelegateProtocol <NSObject>
- (User*) viewControllerUser:(UserViewControllerBase*)viewController;
- (void) viewController:(UserViewControllerBase*)viewController dismissWithUpdatedUser:(User*)user;
- (void) viewController:(UserViewControllerBase*)viewController dismissWithDeletedUser:(User*)user;
- (void) viewController:(UserViewControllerBase*)viewController dismissWithCreatedUser:(User*)user;
@end

This protocol reflects the basic CRUD methods (Create, Read, Update, Delete).

Again, we don't want the Detail View Controller itself to perform these data methods, but instead this will be performed by an instance implementing the UserDataSourceDelegateProtocol. The detail view controller has a property of this delegate, and it sends these "data tasks" to the delegate.

There may be several detail view controllers, all subclasses of abstract class UserViewControllerBase which handle the show, edit and create tasks. Deletion of a user can be performed in the table view and in the "Show User" view controller:

  • ShowUserViewController
  • EditUserViewController
  • NewUserViewController

For example The EditUserViewController will send viewController:dismissWithUpdatedUser: when the user tabs the "back" button AND if the user has modified the user object. Now, the delegate may or may not allow to dismiss the detail view. It may disallow it when there are validation errors for example.

The UserDataSourceDelegateProtocol protocol may be implemented in the root view controller, the table view controller for example. However, a separate class whose sole responsibility is to handle data tasks may be more appropriate. In the sample below, the table view controller will also be this data handler.

The UserDataSourceDelegateProtocol may be defined in an extra header.

In UsersTableViewController.m:

#import "UserDataSourceDelegateProtocol.h"
#import "ShowUserViewController.h"

@interface UsersTableViewController () <UserDataSourceDelegateProtocol>
@property (nonatomic, readonly) NSArray* users;
@end

// This delegate will be called when the detail view controller request
// the user object which shall be displayed.
- (User*) viewControllerUser:(UserViewControllerBase*)viewController {
return [self.users objectInListAtIndex:[self.tableView indexPathForSelectedRow].row];
}

Here, the Table View Controller configures the Show User Detail View Controller:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:UserShowSegueID])
{
ShowUserViewController* showViewController = segue.destinationViewController;
showViewController.delegate = self; // Data Source Handler is self
}
}

The "Edit User" view controller is usually a destination view controller of the "Show User" view controller, which gets viewed when the user tabs the "Edit" button.

The "Show User" view controller will setup the delegate for the
"Edit User" view controller gets the same delegate:

In ShowUserViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:UserEditSegueID])
{
EditUserViewController* editViewController = segue.destinationViewController;
editViewController.delegate = self.delegate; // pass through the data source hanlder
}
}

The data delegate may handle an updated user as follows:

In UsersTableViewController.m:

- (void) viewController:(UserViewControllerBase*)viewController
dismissWithUpdatedUser:(User*)user {
if (/* is valid user and can be saved */) {
[viewController.presentingViewController dismissViewControllerAnimated:YES
completion:nil];
}
}

How do I pass information between storyboard segues?

Use the prepareForSegue:sender: method to pass data from the source to destination view controller:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:...]) {
MyViewController *controller = (MyViewController *)segue.destinationViewController;
controller.myProperty1 = ...;
controller.myProperty2 = ...;
}
}

Passing Data between view Controllers Using a segue from a view embedded in a navigation controller to a tabbarcontroller

This is my view controller where you can check that I am sending 5 to tabbar first viewcontroller:

   class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.performSegue(withIdentifier: "segueIdentifier", sender: self)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let barViewControllers = segue.destination as! UITabBarController
let destinationNv = barViewControllers.viewControllers?[0] as! UINavigationController
let destinationViewController = destinationNv.viewControllers[0] as! FirstViewController
destinationViewController.currentBalance = 5
}
}

Now You can check my firstview controller where you can check that what value we are getting.

class FirstViewController: UIViewController {

var currentBalance = 0
override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
print(currentBalance)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

Now, You can check my console and storyboard:
enter image description here

enter image description here

Pass data from one controller to another via segue and Navigation on different storyboard, Swift, iOS, Xcode

You must take into account the navigation controller in the next storyboard, so your prepare(for: sender:) function should look like this:

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = (segue.destination as! UINavigationController).topViewController as! DestinationViewController
vc!.projectsSentT = ProjecctProd
vc!.logedInAsT = "Prod"
}

where DestinationViewController is the name of the second view controller embedded in the navigationController. And use segue.destination not and instance of the viewController directly.

Passing data between two different storyboards

1/ A good way to do this is to make a separate model object that can be equally addressed from both locations. And the simplest way to do that is to add a property to the @interface section of your AppDelegate.h file, eg

  @property (nonatomic, strong) NSString* sharedString;

To set and get it, you need to add typed access to the AppDelegate in any file that needs it:

  #include AppDelegate.h

Then you can use...

  AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

//to set (eg in A controller)
appDelegate.sharedString = string;

//to get (eg in B controller)
NSString* string = appDelegate.sharedString;

As an alternative to a property, you could use a static variable in a header file:

 static NSString* staticString;

Which would be accessible to any object that #imports the header file. Not really the Objective-C way though.

For more elaborate cases, you may want to create a singleton object to access your model data.

2/ Try:

  [self performSegueWithIdentifier: @"secondViewSegue" sender:self];

Ensure that the Segue is wired from your viewController, not it's Navigation Controller.

Pass data through segue

Swift works exactly the same way as Obj-C but is reworked in the new language. I don't have a lot of information from your post but let's give a name to each TableViewController to help with my explanation.

HomeTableViewController (this is the screenshot you have above)

PlayerTableViewController (this is the player screen you want to travel to)

With that said, in PlayerTableViewController you need to have a variable that will store the passed data. Just under your class declaration have something like this (if you intend to store the struct as a single object rather than the array:

class PlayerTableViewController: UITableViewController {

var programVar : Program?

//the rest of the class methods....

After that there are two ways you can send data to the new TableViewController.

1) Using prepareForSegue

At the bottom of HomeTableViewController you will use the prepareForSegue methods to pass the data. Here is an example of the code you'll use:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

// Create a variable that you want to send
var newProgramVar = Program(category: "Some", name: "Text")

// Create a new variable to store the instance of PlayerTableViewController
let destinationVC = segue.destinationViewController as PlayerTableViewController
destinationVC.programVar = newProgramVar
}
}

Once PlayerTableViewController has loaded the variable will be already set and usable

2) Using didSelectRowAtIndexPath

If specific data needs to be sent based on which cell is selected you can use didSelectRowAtIndexPath. For this to work, you need to give your segue a name in the storyboard view (let me know if you need to know how to do this too).

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

// Create a variable that you want to send based on the destination view controller
// You can get a reference to the data by using indexPath shown below
let selectedProgram = programy[indexPath.row]

// Create an instance of PlayerTableViewController and pass the variable
let destinationVC = PlayerTableViewController()
destinationVC.programVar = selectedProgram

// Let's assume that the segue name is called playerSegue
// This will perform the segue and pre-load the variable for you to use
destinationVC.performSegueWithIdentifier("playerSegue", sender: self)
}

Let me know if you need any other info on this

How can I pass information between storyboard scenes using segues?

in your Scene2ViewController header, you can put a @property of your current view controller so you have acces to its public variables

so in Scene2ViewController.h:

#import "YourPreviousViewController.h"

//add this to your @interface
@property (strong) YourPreviousViewController *parent;

then in your current viewcontroller.m

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
Scene2ViewController *destination = [segue destinationViewController];
destination.parent = self;
}

then you can run your returned method from Scene2ViewController or you can just manipulate the public variables on your current viewcontroller ie parent

edit: i have never seen a returned method like that before, you could just make a method that will set the variables you need in your current view controller, then in Scene2ViewController you can implement - (void) viewWillDisappear { } which will run when you press back or segue to another view

//Scene2ViewController.m
- (void) viewWillDisappear {
[self.parent returnAndSetBool:someBool];

//or if myBoolean in the previous view controller is a public variable you can just go:
//self.parent.myBoolen = someBool;
}

//CurrentViewController.m
- (void) returnAndSetBool: (bool) passbackVariable {
self.myBoolean = passbackVariable;
}

How to Pass information Back in iOS when reversing a Segue using Swift?

If you were presenting a modal view with Done and Cancel buttons (sort of like a picker), grabbing the value during an unwind segue method would probably be the easiest.

Given that you want to use the navigation controller's native Back button, the best practice would probably be to implement a protocol that VC One can conform to, and then update VC One as soon as the data on VC Two is selected. Something like:

In VCTwo.swift:

protocol VCTwoDelegate {
func updateData(data: String)
}

class VCTwo : UIViewController {
var delegate: VCTwoDelegate?
...
@IBAction func choiceMade(sender: AnyObject) {
// do the things
self.delegate?.updateData(self.data)
}
...
}

and in VCOne.swift:

class VCOne: ViewController {
...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "VCTwoSegue" {
(segue.destinationViewController as VCTwo).delegate = self
}
}
...
}

extension VCOne: VCTwoDelegate {
func updateData(data: String) {
self.internalData = data
}
}

Passing variables between Storyboards without Segues - Swift

I'm assuming that the viewController that you want to pass the data to is a custom viewController. In that case, use this amended code here:

var storyboard = UIStoryboard(name: "IDEInterface", bundle: nil)
var controller = storyboard.instantiateViewControllerWithIdentifier("IDENavController") as! MyCustomViewController

controller.exercisedPassed = "Ex1"

self.presentViewController(controller, animated: true, completion: nil)

Pass the data between the view controllers via prepare without connecting Show segue in the storyboard

Prepare for segue method will not be called in this scenario
You can use the following code:

@objc func clickOnButton() {
let chapterVC = self.storyboard?.instantiateViewController(withIdentifier: "ChapterViewController") as! ChapterViewController
chapterVC.bookNo = self.bookNo
self.present(chapterVC, animated: true, completion: nil)
}


Related Topics



Leave a reply



Submit