2

I have 2 view controllers . First one is UIview controller and second one is table view controller. I want to send data 2nd (table view controller) to first(uiview Controller) after the selection of rows of 2nd view controller. For this i have written a delegate protocol. But my delegate protocol is not working... I figured out the problem.The object of second view controller that i am creating.

address = [[second_viewcontroller alloc] init];
address.delegate = self;

is different from self of second_viewcontroller view controller page. How to make this two same.

self = [[second_viewcontroller alloc] init];
VRAwesome
  • 4,721
  • 5
  • 27
  • 52
Manzoor
  • 57
  • 3
  • You don't need to create a delegate to transfer data from one controller to other. – Harsh Aug 16 '16 at 02:01
  • user `NSUserDefaults` , you can do easily with this – caldera.sac Aug 16 '16 at 03:28
  • how you are pushing from First View to Second view can you show me the code so I can help you on this? – CodeChanger Aug 16 '16 at 04:49
  • Very good tried solution http://stackoverflow.com/questions/33748950/ios-pass-data-back-from-viewcontroller2-to-viewcontroller-1-with-presentmodalseg/33751609#33751609 – user3182143 Aug 16 '16 at 05:24
  • Manzoor brother please check above link.I have used Notification and Custom Delegate for passing data from SecondViewController to FirstViewController.It works perfectly. – user3182143 Aug 16 '16 at 05:26
  • how are you navigating from FirstViewController to SecondViewController..? Is it programmatically using performSegue/ pushViewController or through a storyboard Segue..? Your answer depends on that. – Jen Jose Aug 16 '16 at 12:13

4 Answers4

2

your problem my delegate protocol is not working... I figured out the problem.The object of second view controller that I am creating. address=[[second_viewcontroller alloc]init]; address.delegate=self; is different from self of second_viewcontroller view controller page.

It's clear say that way you create the second_viewcontroller object is not right.

You have to create the object from ViewController storyboard identifier. First give the Storyboard ID to ViewController from Storyboard.Follow this step to Giving the Storyboard ID.

  1. Select the particular ViewController in Storyboard.
  2. Go to IdentityInspector.
  3. Under IdentityInspector, There is identity section and add the Storyboard ID In "Storyboard ID" Field.

Syntax For Creating a ViewController Object.

Second_viewController *aVC = [self.storyboard instantiateViewControllerWithIdentifier:@"Second_viewController"];
aVC.delegate = self;
Chetan kasundra
  • 508
  • 2
  • 14
  • UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Reservation" bundle:nil]; reservationTableViewController *aVC = [storyboard instantiateViewControllerWithIdentifier:@"hotel_addreess"]; NSLog(@"%@address",aVC); – Manzoor Aug 16 '16 at 06:06
  • Have you checked that delegate is working or not with this code ? If not can you post your code of how to create the delegate and where you use? – Chetan kasundra Aug 16 '16 at 06:53
1

I assume that you are calling the Second_ViewController from storyboard instead of doing programmatically.

In that case, the correct instance of Second_ViewController can be accessed in prepareForSegue. For that, you need to set a Storyboard segue identifier, eg "Second_ViewController"

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"Second_ViewController"]) {

        SecondViewController *aSecVC = segue.destinationViewController;
        // Register the Delegate to self.So when we call the delegate method from secondVC, SendMessage will be call of ViewController
        aSecVC.delegate = self;

    }
}

If you use alloc-init or instantiateViewControllerWithIdentifier, when you are using a storyboard push segue, it will create another instance.

Jen Jose
  • 3,995
  • 2
  • 19
  • 36
0

Yes as your instantiating a new instance of the second view controller. From what I could make out from your question, I guess if you obtain the instance of your secondViewController from the Navigation Stack it should work

Rakshith Nandish
  • 599
  • 3
  • 13
0

I created a sample project for you to get the basic knowledge of how to pass data backward using NSUserDefaults. try this in GitHub. hope this will help to you. project url Pass data backward using NSUserDefaults in Objective-C

caldera.sac
  • 4,918
  • 7
  • 37
  • 69
  • I don't think its good practice to use `NSUserDefaults` to pass one view controller value to another for that you need to use `Delegate` or pass through property. – CodeChanger Aug 16 '16 at 04:52
  • its because we are storing sensitive data (e.g user detail) or small bunch of data in `NSUserDefaults` but to transfer One class data to another class we have many options so better to use that one in replace of use `NSUserDefaults`. – CodeChanger Aug 16 '16 at 06:02
  • In above question didn't mention about any sensitive data, he just ask for pass data backward only @Dhanesh – caldera.sac Aug 16 '16 at 06:09
  • yes but he mention that he wants to pass one Class data to another Class so its better to use Delegate for this and this is just one suggestion to be in practice so i commented here thats it. – CodeChanger Aug 16 '16 at 06:25