Manzoor Manzoor - 3 months ago 12
Objective-C Question

ios Delegate Objective-c data sending from 2nd view controller to 1st view controller

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];

Answer

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.