Pass data in modal segue in storyboard?

My real problem is i want to pass data from childViewController to parentViewController in storyboard by modal segue.

Code

parentViewController.h

@property (strong, nonatomic)NSMutableArray *address;

childViewController.m

parentViewController *parent=(parentViewController *)self.presentingViewController; [email protected]"Hello World";

This code throw exception like

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainTabbarViewController setAddress:]: unrecognized selector sent to instance 0x7fa070e6b910'

storyboard structure

TabbarController-> NavigationController-> parentViewController-> childViewController

Thank you in Advance.

--------------Solutions-------------

There is no way to get a reference to the segue that created you. You could create a property (sourceVC in my example) in the destination controller, and assign self to this property in the prepareForSegue method (in the source view controller):

[(ChildViewController *)segue.destinationViewController sourceVC] = self;

Anyway, I think it's a better solution to use a delegate.

* In ChildViewController.h *

@protocol ChildDelegate <NSObject>
- (void)postAddress:(NSString*)ad;
@end

@interface ChildViewController : UIViewController
@property (weak, nonatomic) id <ChildDelegate> delegate;
@end

* In ParentViewController.h *

@interface ParentViewController : UIViewController <ChildDelegate>
@end

* In ParentViewController.m *

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
ChildViewController *vc = [segue destinationViewController];
vc.delegate = self;
}

* In ChildViewController.m *

- (void)changeAddress:(NSString*)ad
{
if ([self.delegate respondsToSelector:@selector(postAddress:)])
{
// Tell the delegate something.
[self.delegate postAddress:@"New Address"];
}
}

the proper way to do that is using delegate

in ChildViewController.h

@protocol ChildDelegate
- (void)postAddress:(NSString *)address;
@end
@interface ChildViewController
@property (nonAtomic, assign) id<ChildDelegate> delegate;
@end

in ChildViewController.m

[self.delegate postAddress:address];

in ParentViewController.h

@interface ParentViewController <ChildDelegate>
@end

in ParentViewController.m

// presenting childViewController
- (void)presentChildViewController {
ChildViewController *childViewController = [[ChildViewController alloc]init];
childViewController.delegate = self;
[self presentViewController:childViewController animated:YES completion:nil];
}

// delegate method
- (void)postAddress:(NSString *)address{
// add you code here
}

You cannot pass the data back to the parent view controller like this. Delegation is the best way to implement this. Please have a look on the following link where it is explained in detail.

passing-data-between-view-controllers

Category:ios Time:2018-12-17 Views:0

Related post

  • Passing data from modal navigation controller to parent controller? 2009-05-21

    I'm starting out in iPhone development, and I'm trying to do a Contacts-style controller to add items to a table. I've got the two controllers designed, but I can't seem to figure out how to pass data between them. How can I get data from the modal c

  • Passing ManagedObjectContext to Modal created in Storyboard 2012-01-06

    I have successfully setup my ManagedObjectContext in my AppDelegate. I have then passed it down to the first ViewController (which is in a NavigationController) via rootViewController followed by viewControllers.objectAtIndex. I do all of this in App

  • Pass data to modal forms (JQuery UI) 2011-06-30

    I'm currently loading a modal form when I'm inserting a new node into a jstree using the following code: $("#structureForm").dialog({ autoOpen: false, height: 120, width: 300, modal: true, buttons: { "Create new structure": function() { $("#structure

  • Passing Data between View Controllers 2011-03-06

    I'm new to iOS and Objective-C and the whole MVC paradigm and I'm stuck with the following: I have a view that acts as a data entry form and I want to give the user the option to select multiple products. The products are listed on another view with

  • how to pass data from one screen to another screen using Segue 2012-01-02

    I have 2 views A and B. View A contains a table of 1 section with 2 rows. Each row has each own picture....My task is how to display in view B a picture of row 1 when you click on row one. If you click on row 2, view B will display picture of row 2.

  • Pass data from a table to a webview USING SEGUES 2012-01-29

    I have a table based off Sam's Teach Yourself iOS Development's FlowerViewController, that, under didSelectRowAtIndesPath it goes to a website in a new nib (I tweaked part of the passing data). MY QUESTION: I would like to update this to, instead of

  • Difference between Modal and Push segue in storyboards 2012-02-22

    Can someone explain to me what is the exact difference between modal and push segue? I know that when we use push it gets added to a stack, so when we keep using push it keeps occupying memory? Can someone please show me how these two are implemented

  • Xcode 4.2 & Storyboard, how to pass data between views? Existing code error 2012-03-12

    I'm trying to learn how to pass data between views. Say set a label in the second view from text entered into a text field on the first view. I basically have tried making a string in the second view and then when switching from the first view to the

  • How to pass data to another storyboard in xCode? 2012-03-14

    I'm new to iPhone application development I have a question about how to pass data from one view (xib file) to another storyboard? for example, I have "welcome.xib" which contains nameTextfield and nextButton ,user will type their name into nameTextf

  • Issue with passing data with segue to another DetailTableViewController 2012-04-08

    I want to pass data from a TableViewController cell to a DetailViewContoller (UITableViewController) using a segue (ShowADVDetail) in Storyboard. I've got a NSMutableArray 'stories' containing a parsed RSS feed. I am using the following prepareForSeg

  • iOS Segue And Passing Data Back 2012-01-02

    I have two controllers in the storyboard, embedded in a NavigationController, and there is a segue to switch between these. Passing data from the first controller to the second one is pretty straightforward by implementing prepareForSegue, and set th

  • What is the best way to pass data between a MainFrame (or Main Dialog) and a Modal Dialog? 2008-11-01

    I need a modal dialog to gather some user input. I then need the same data to be consumed by the application MainFrame. Usually my Modal Dialog would have a pointer to some DataType able to store what I need, and I'd be passing this object by referen

  • Passing Data in and out of CKEditor / jQuery-UI Modal Dialog 2010-04-19

    Greetings, Overflowers! I am writing an application in PHP that will allow for editing various blocks of content via a button that loads a CKEditor Instance inside a jQuery-UI Modal Dialog box. I've got the button working to launch the Dialog, as sho

  • how do I pass data to a child UINavigationController which is presented modally (i.e. via initWithRootViewController) 2011-03-27

    How do I pass data to a child UINavigationController which is presented modally via "[[UINavigationController alloc] initWithRootViewController:newItemController];"? That is the way with this method of creating the child controller (i.e. newItemContr

  • How to create custom modal segue in 4.2 Xcode using storyboard 2011-11-11

    I'm using storyboard to create my new iPad project. I want use the custom modal segue to have better transitions between views. My question is how do I use the custom modal and is there any tutorials out there that show using custom modal segues? ---

  • ios storyboard passing data navigationViewController 2011-12-08

    I am new to obj-c and ios. I have problem with proper passing data between view's but not in standard way. Picture describing my problem: I performSegueWithIdentifier with one of two segue identifiers and then in I want to pass data to ViewController

  • Passing Core Data Results Between View Controllers / Storyboard Scenes 2012-03-26

    Fairly new to Objective C and trying to work within Storyboards given it's the newest and greatest from Apple. I'm curious, at a high level, what the preferred or best practice is for passing data between storyboard scenes where Core Data is the back

  • How do I pass data to a rootViewController of a UINavigationController in an instantiated Storyboard? 2014-05-07

    Here is my code. The problem is that title isn't getting passed to PinTableViewController. From my original view: NSString *title = @"testing123"; UIStoryboard *tableViewStoryboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil]; UINavig

  • MVC + JQuery : Pass data from textfield to a modal 2014-09-04

    I'm trying to pass some user entered data from a bootstrap form and push the values to a modal in an MVC4 project in visual studio 2013, tried reading a few topics but I'm struggling to find a simple answer. How do i get a value from my boostrap form

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.745 (s). 13 q(s)