2

In UINavigationController this is child controller

.h

@protocol childProtocol <NSObject>

-(void)childMethod:(NSArray*)params;

@end

@property (strong, nonatomic) id<childProtocol>childDelegate;

@property (weak, nonatomic) parentVC *pVC;

.m 

if([self.childDelegate respondsToSelector:@selector(childMethod:)]) {

    [self.childDelegate performSelector:@selector(childMethod:) withObject:self.arry];    

}

This is my parent controller

.m

-(void)childMethod:(NSArray *)params {
    // some work 
}

...

 childVC *cVC = [[childVC alloc]init];
    cVC.pVC = self;

But childMethod: is not getting called so I searched on internet and got this post UINavigationControllers: How to pass value to higher (parent?) controller in stack?

I tried to create a weak reference but dont know how to use to make delegate pass data from child to parent?

Community
  • 1
  • 1
S.J
  • 3,063
  • 3
  • 33
  • 66
  • Show us how you assigned the delegate to your parent view controller. – Zen Jun 20 '13 at 04:47
  • @Zen ParentViewController – S.J Jun 20 '13 at 05:07
  • What I meant was how is the pVC property is conforming to the `childDelegate`. i.e. if there is something like `self.childDelegate = self.pVC;` or anything like that telling the `pVC` that it has to act when the `childDelegate` is revoked? – Zen Jun 20 '13 at 10:39

3 Answers3

4

Try this. Check the sample project attached

ParentViewController.h

#import <UIKit/UIKit.h>

@interface ParentViewController : UIViewController

- (void)passData:(NSString *)strText;

@end

ParentViewController.m

- (IBAction)btnGoToSecondView:(id)sender {
    ChildViewController *secondVC = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil];
    secondVC.delegate = self;
    [self presentViewController:secondVC animated:YES completion:nil];

}

- (void)passData:(NSString *)strText {
    NSLog(@"Data Passed = %@",strText);
}

ChildViewController.h

#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@class ParentViewController;

@interface ChildViewController : UIViewController

@property(nonatomic, assign) ParentViewController *delegate;

@end

ChildViewController.m

- (IBAction)btnPassDataBack:(id)sender {
    if([self.delegate respondsToSelector:@selector(passData:)]) {
        [self.delegate passData:@"Hello"];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

Sample Project

icodebuster
  • 8,890
  • 7
  • 62
  • 65
2

This is child controller.h

@protocol childProtocol <NSObject>
    -(void)childMethod:(NSArray*)params;

@end

@property (strong, nonatomic) id<childProtocol>childDelegate;

@property (weak, nonatomic) parentVC *pVC;

.m

if([self.childDelegate respondsToSelector:@selector(childMethod:)]) {

    [self.childDelegate performSelector:@selector(childMethod:) withObject:self.arry];    

}

This is my parent controller.h

#import <UIKit/UIKit.h>
#import "ChildController.h"


@interface perentController : UIViewController < childProtocol >

.m

- (void)childMethod:(NSArray *)params {
        // some work 
}

EDITED :

And Dont Forget to add childViewOBJ.childDelegate = self; at the time of create ChildViewController's object. such like,

childVC *cVC = [[childVC alloc]init];
cVC.childDelegate = self;
cVC.pVC = self;
[self presentModalViewController:cVC animated:YES];

For More information about How to create/use of Protocol.

Community
  • 1
  • 1
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • why you copy paste my question as answer, if you are thing that I didnt did this UIViewController < childProtocol > so thats wrong I have already implemented this. – S.J Jun 20 '13 at 04:36
  • @S.J- First check it, it is not totally same as your question.. i just want to help you, nothing any more okay.... :) – iPatel Jun 20 '13 at 04:38
  • please check my question again I corrected my copy paste mistake. – S.J Jun 20 '13 at 04:45
  • @S.J just follow my edited answer :) and if still then any problem then follo link that i added to my answer :) – iPatel Jun 20 '13 at 04:48
  • I know how to use delegates and in my init method I have already don this self.cVC = [[childVC alloc]init]; self.cVC.childDelegate = self; – S.J Jun 20 '13 at 05:05
0

First of all, you are not checking for the same selector as you declared in your protocol declaration so it won't respond to that. You declared the method childMethod: whereas you are checking if your childDelegate responds to myMethod: selector which does not so it won't go into the if condition.

Also the parent view controller is missing the implementation the method childMethod: in its .m. Implement that in your parent view controller or it will crash because of not finding the exact selector definition.

Since you are using a UINavigationController, the parent view controller won't be lost till the child view controller exist so the childDelegate property must not be strong unless you intend to hold onto your delegate in child view controller for some reason.

Zen
  • 3,047
  • 1
  • 20
  • 18