Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass data back to previous viewcontroller

I am trying to pass data BACK TO previous viewController.

Does anyone know how to pass data back from ViewController B to ViewController A? So I want a string to go 'from' BIDAddTypeOfDealViewController to BIDDCCreateViewController. A user edits viewController B and I want that edited data back in ViewController A where I then use it.

I am using the 'passing data back' section of this answer. How mine differs: Point 3 and 6 just mentions when views are popped so I have put that code in viewWillDisappear. I think that is correct? Also on Point 6 I did not initialise with nib as that is old. I'm using storyboards. And I did not add that last line as I do not believe I would have to push it. Pressing a button on my storyboard already takes me forward.

I think the problem may arise in BIDDCCreateViewController, I have the method but I cannot run it. To run a method it should go [self method]. I am unable to do that. Well that is just what I am guessing.

It compiles and runs fine just nothing is logged, so I don't know if it works.

UPDATE: I am unable to get the 'sendDataToA' method to execute.

#import <UIKit/UIKit.h> #import "BIDAddTypeOfDealViewController.h"   @interface BIDDCCreateViewController : UIViewController  @property (strong, nonatomic) NSString *placeId; - (IBAction)gotoBViewController:(id)sender; @end   #import "BIDDCCreateViewController.h" #import "BIDAddTypeOfDealViewController.h"  @implementation BIDDCCreateViewController  - (void)viewDidLoad {     [super viewDidLoad];     // Do any additional setup after loading the view.     NSLog(@"SUCCESSFULLY PASSED PLACE ID: %@", self.placeId); }  -(void)sendDataToA:(NSString *)myStringData {      NSLog(@"Inside sendDataToA");     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your string Data Showing" message:myStringData delegate:self cancelButtonTitle:@"Ok " otherButtonTitles:nil];     [alert show]; }  - (IBAction)gotoBViewController:(id)sender {     NSLog(@"pressed");     BIDAddTypeOfDealViewController *bidAddType = [[BIDAddTypeOfDealViewController alloc]init];     bidAddType.delegate = self;  } @end   @protocol senddataProtocol <NSObject> -(void)sendDataToA:(NSString *)myStringData; @end  #import <UIKit/UIKit.h> @interface BIDAddTypeOfDealViewController : UIViewController <UITextFieldDelegate>//Using this delegate for data a user inputs @property(nonatomic,assign)id delegate; //other textfield outlets not relevant - (IBAction)chooseDiscountDeal:(id)sender; @end  #import "BIDAddTypeOfDealViewController.h"  @interface BIDAddTypeOfDealViewController ()  @end  @implementation BIDAddTypeOfDealViewController @synthesize delegate;  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];     if (self) {         // Custom initialization     }     return self; }  - (void)viewDidLoad {     [super viewDidLoad]; }  -(void)viewWillDisappear:(BOOL)animated {     [delegate sendDataToA:@"Apple"]; } @end 
like image 925
Anthony Avatar asked Oct 13 '13 09:10

Anthony


People also ask

How do I transfer data from one ViewController to another?

Control + click the UI element you are going to use to make the bridge and drag to the second View Controller. Select the “Show” option from the “Action Segue” menu. Control + click the button and drag to the second ViewController, then select “Show.”


1 Answers

You can use a delegate. So in your ViewController B you need to create a protocol that sends data back to your ViewController A. Your ViewController A would become a delegate of ViewController B.

If you are new to objective C, please look at What is Delegate.

Create protocol in ViewControllerB.h :

#import <UIKit/UIKit.h>  @protocol senddataProtocol <NSObject>  -(void)sendDataToA:(NSArray *)array; //I am thinking my data is NSArray, you can use another object for store your information.   @end  @interface ViewControllerB : UIViewController  @property(nonatomic,assign)id delegate; 

ViewControllerB.m

@synthesize delegate; -(void)viewWillDisappear:(BOOL)animated {      [delegate sendDataToA:yourdata];  } 

in your ViewControllerA : when you go to ViewControllerB

ViewControllerA *acontollerobject=[[ViewControllerA alloc] initWithNibName:@"ViewControllerA" bundle:nil]; acontollerobject.delegate=self; // protocol listener [self.navigationController pushViewController:acontollerobject animated:YES]; 

and define your function:

-(void)sendDataToA:(NSArray *)array {    // data will come here inside of ViewControllerA } 

Edited :

You can See this example : How you can Pass data back to previous viewcontroller: Tutorial link

like image 192
Erhan Demirci Avatar answered Sep 23 '22 19:09

Erhan Demirci