无法将数据传递到上一个viewcontroller

时间:2017-02-11 11:57:22

标签: ios objective-c

我试图实现委托设计模式。

我有两个视图控制器,如下所示

  1. CallViewViewController
  2. CEPeoplePickerNavigationController
  3. 这是CallViewViewController

    的界面定义
    @interface CallViewViewController ()<CEPeoplePickerNavigationControllerDelegate>{
    
    }
    
    @property(nonatomic)CustomMessageClass * customMessageClassObject;
    
    @end
    

    在我的实现中,我当然实现了委托方法

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

    这是我的第二个类的接口定义

    @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
    

    当用户按下提交按钮时,我执行以下代码,

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

    但是数据没有传回给前一个视图控制器。为什么这样?

    更新

    我尝试使用以下代码将表单首先移动到第二个视图控制器,

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

    但它会引发以下错误,

    由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [UINavigationController setPeoplePickerDelegate:]:无法识别的选择器发送到实例0x101054800'

2 个答案:

答案 0 :(得分:2)

当您从CallViewViewController移动到CEPeoplePickerNavigationController时,必须有某种导航,您可以在其中推送或应用segue进入下一个VC。

只需添加这行代码 -

如果您使用的是segue -

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

如果您正在使用实例化 -

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

完成此操作后,它将响应来自下一个控制器的代理

修改

请将您的CEPeoplePickerNavigationController .h代码清除到以下

 @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

答案 1 :(得分:2)

您只需使用自定义代理

传递数据

在CEPeoplePickerNavigationController的.h文件中写下以下代码

@class CEPeoplePickerNavigationController;

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

@interface CEPeoplePickerNavigationController : UIViewController

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

@end

在您转移到上一个视图控制器事件时,您需要将代码放在

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

在CallViewViewController上,您只需要在.m文件中添加以下代码

#import "CEPeoplePickerNavigationController.h"

@interface ViewController ()< CEPeoplePickerNavigationController Delegate>

在前一个视图控制器上添加方法声明

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

确保当您导航到CEPeoplePickerNavigationController时,您需要将委托分配给self,如下所示

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