2017-02-11 113 views
0

我试图实现委托设计模式。无法传递数据到前一个视图控制器

我有两个视图控制器作为followsm

  1. CallViewViewController
  2. CEPeoplePickerNavigationController

这是CallViewViewController

@interface CallViewViewController()<CEPeoplePickerNavigationControllerDelegate>{ 

} 

@property(nonatomic)CustomMessageClass * customMessageClassObject; 

@end 

我在执行我的接口定义,我有OF-课程实施委托方法

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

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

UPDATE

我尝试下面的代码首先移动形式第二视图控制器,

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

但它引发以下错误,

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

+0

您的第一个视图控制器是否接受委托? –

+0

不...第一个视图控制器委托不被称为 –

+0

我使用instantiateViewControllerWithIdentifier –

回答

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> 

Add方法声明以前的观点控制器上

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

确保在您导航到CEPeoplePickerNavigationController需要分配委托自我如下

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

PLZ检查http://stackoverflow.com/questions/42197175/unable-to-receive-callback-from-second-view-controller –

2

当您从CallViewViewController移动到CEPeoplePickerNavigationController时,必须有某种导航方式,您正在推送或应用继续前进下一个VC。

只需添加这行代码有 -

如果使用赛格瑞 -

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 
+0

我使用instantiateViewControllerWithIdentifier –

+0

编辑答案相应 –

+0

你请...... –

相关问题