user3600801
user3600801

Reputation:

Unable to pass data to the previous viewcontroller

Im trying to implement delegate design pattern.

I have two view controllers as followsm

  1. CallViewViewController
  2. CEPeoplePickerNavigationController

This is my interface definition of CallViewViewController

@interface CallViewViewController ()<CEPeoplePickerNavigationControllerDelegate>{

}

@property(nonatomic)CustomMessageClass * customMessageClassObject;

@end

In my implementation, i have of-course implemented the delegate method

-(void)cePeoplePickerNavigationController:(CEPeoplePickerNavigationController *)peoplePicker didFinishPickingPeople:(NSArray *)peopleArg values:(NSDictionary *)valuesArg
{
    NSLog(@"can populate the tableview");
}

This is the interface definition of my second class,

@protocol CEPeoplePickerNavigationControllerDelegate;

@interface CEPeoplePickerNavigationController : UIViewController <UITableViewDelegate,UITableViewDataSource>{
    id<CEPeoplePickerNavigationControllerDelegate> peoplePickerDelegate;
    ABAddressBookRef addressBook;
}

@property (nonatomic, assign) id<CEPeoplePickerNavigationControllerDelegate> peoplePickerDelegate;
@property (nonatomic, retain) CEPeoplePickerNavigationController *ppnc;

@end


@protocol CEPeoplePickerNavigationControllerDelegate <NSObject>

- (void)cePeoplePickerNavigationController:(CEPeoplePickerNavigationController *)peoplePicker didFinishPickingPeople:(NSArray*)people values:(NSDictionary *)values;

@end

when the user presses submit button,Im executing the following code,

 if ([self.ppnc.peoplePickerDelegate respondsToSelector:@selector(cePeoplePickerNavigationController:didFinishPickingPeople:values:)])
        [self.ppnc.peoplePickerDelegate cePeoplePickerNavigationController:self.ppnc didFinishPickingPeople:sortedPeople values:[NSDictionary dictionaryWithDictionary:self.selectedValues]]; 

But the data is not being passed back to the previous view controller. Why so?

UPDATE

i tried the following code to move form first to second view controller,

CEPeoplePickerNavigationController *nextVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PeoplePickerNavigationController"];
    nextVC.peoplePickerDelegate = self;
    [self presentViewController:nextVC animated:YES completion:nil];

but it throws the following error,

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setPeoplePickerDelegate:]: unrecognized selector sent to instance 0x101054800'

Upvotes: 0

Views: 76

Answers (2)

Maulik Pandya
Maulik Pandya

Reputation: 2220

You just need to pass data using Custom Delegate

Write Below code on your CEPeoplePickerNavigationController's .h file

@class CEPeoplePickerNavigationController;

@protocol CEPeoplePickerNavigationController <NSObject>
- (void)previousViewController:(CEPeoplePickerNavigationController *)controller itemToSend:(NSString *)item;
@end

@interface CEPeoplePickerNavigationController : UIViewController

@property (nonatomic, weak) id < CEPeoplePickerNavigationController Delegate> delegate;

@end

on your moving to previous view controller event you need to put below code

[self.delegate previousViewController:self itemToSend:@"From Previous VC"];
[self.navigationController popViewControllerAnimated:true];

On your CallViewViewController you just need to add below code on .m file

#import "CEPeoplePickerNavigationController.h"

@interface ViewController ()< CEPeoplePickerNavigationController Delegate>

Add method declare on previous view controller

- (void)previousViewController:(CEPeoplePickerNavigationController *)controller itemToSend:(NSString *)item
{
    NSLog(@"from CEPeoplePickerNavigationController %@",item);
}

make sure when you navigate to CEPeoplePickerNavigationController you need to assign delegate to self as below

CEPeoplePickerNavigationController *objVC = [self.storyboard instantiateViewControllerWithIdentifier:@"CEPeoplePickerNavigationController"];
objVC.delegate = self;
[self.navigationController pushViewController:infoController animated:YES];

Upvotes: 1

Saheb Roy
Saheb Roy

Reputation: 5957

When you are moving from CallViewViewController to CEPeoplePickerNavigationController , there must be some sort of navigation where you are pushing or applying segue to go the next VC.

Just add this line of code there -

If you are using segue --

CEPeoplePickerNavigationController *nextVC = [segue destinationViewController];
nextVC.peoplePickerDelegate = self;

If you are using instantiate --

CEPeoplePickerNavigationController *nextVC = [self.storyboard instantiateWithStoryboardid:@"YOUR STORYBOARD ID"];
nextVC.peoplePickerDelegate = self;

Once this is done, it will respond to the delegate from the next controller

EDIT

Please clear your CEPeoplePickerNavigationController .h code to the following

 @protocol CEPeoplePickerNavigationControllerDelegate <NSObject>

 - (void)cePeoplePickerNavigationController:(CEPeoplePickerNavigationController *)peoplePicker didFinishPickingPeople:(NSArray*)people values:(NSDictionary *)values;

@end


@interface CEPeoplePickerNavigationController : UIViewController <UITableViewDelegate,UITableViewDataSource>{
ABAddressBookRef addressBook;
}

@property (nonatomic, assign) id<CEPeoplePickerNavigationControllerDelegate> peoplePickerDelegate;
@property (nonatomic, retain) CEPeoplePickerNavigationController *ppnc;

@end

Upvotes: 1

Related Questions