2017-02-11 129 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

你請...... –

相關問題