unable to receive callback from second view contro

2019-03-04 13:02发布

问题:

I have got two view controllers. Im trying to pass data to the previous viewcontroller

I have the following code in my second view controller CEPeoplePickerNavigationController

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


@interface CEPeoplePickerNavigationController : UIViewController <UITableViewDelegate,UITableViewDataSource>{
}
@property (nonatomic, retain) id < CEPeoplePickerNavigationControllerDelegate> peoplePickerDelegate;
@end

When the user clicks the done button, following code will be exectied

- (void)doneAction:(id)sender
{
    [self.peoplePickerDelegate previousViewController:self itemToSend:@"From Previous VC"];
    [self dismissViewControllerAnimated:YES completion:nil];
}

In my first view controller, I have the following interface in header file and I have ofcourse implemented the previousViewController method in my first view controller where the data has to be received

@interface CallViewViewController : UIViewController<CEPeoplePickerNavigationControllerDelegate>
@end

When I move from the first view controller to the second view controller, Im using the following code.

CEPeoplePickerNavigationController *nextVc = [[CEPeoplePickerNavigationController alloc] init];
    nextVc.peoplePickerDelegate = self;

    [self presentViewController:nextVc animated:YES completion:nil];

But when the user clicks the done button from the second view controller, Im not receiving any callback in my first view controller. why so?

I have implemented the interface as following,

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

UPDATE:

Following code works

if ([[CallViewViewController new] respondsToSelector:@selector(previousViewController:item:)]) {
    [self.viewCrtrlDelegate previousViewController:self item:@"Here I am"];
    [self dismissViewControllerAnimated:YES completion:nil];
}
else{
   NSLog(@"Your delegate was properly set");
}

But if I try it by following way, it is not working

if ([[self.viewCrtrlDelegate respondsToSelector:@selector(previousViewController:item:)]) {
    [self.viewCrtrlDelegate previousViewController:self item:@"Here I am"];
    [self dismissViewControllerAnimated:YES completion:nil];
}
else{
   NSLog(@"Your delegate was properly set");
}

This is how Im instantiating the view controller.

CEPeoplePickerNavigationController *nextVc = [[CEPeoplePickerNavigationController alloc] init];

    nextVc.viewCrtrlDelegate = self;
    [self presentViewController:nextVc animated:YES completion:nil];

回答1:

Your code seems absolutely correct but to get around all the possibilities. Can you check whether your done button action has a check to know if the delegate methods is available or not in that class?

if ([self.viewCrtrlDelegate respondsToSelector:@selector(previousViewController:item:)]) {
    [self.viewCrtrlDelegate previousViewController:self item:@"Here I am"];
    [self dismissViewControllerAnimated:YES completion:nil];
}
else{
   NSLog(@"Your delegate was properly set");
}