2017-02-11 15 views
0

私はデリゲートデザインパターンを実装しようとしています。前のビューコントロールにデータを渡すことができません

私はfollowsm

  1. ように2つのView Controllerを持ってCallViewViewController
  2. CEPeoplePickerNavigationController

これは私の実装では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 

ユーザーを押すボタン、Imは次のコードを実行して提出する

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

しかし、データ以前のビューコントローラに戻されていません。なぜそうなのか?

UPDATE

iが

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

、第2のビューコントローラに最初のフォームを移動するために、次のコードを試みたが、それは次のエラーをスローし、

は、キャッチされない例外によるアプリ '終了NSInvalidArgumentException '、reason:' - [UINavigationController setPeoplePickerDelegate:]:インスタンスに送信された認識できないセレクタ0x101054800 '

+0

最初のView Controllerはデリゲートを受け入れていますか? –

+0

なし...最初のビューコントローラのデリゲートは、私は完全なソリューションが、 'peoplePickerDelegate'に' self'を渡さないでくださいアプリ –

+0

イム呼び出されていません。 'CallViewViewController'オブジェクトを渡します。 –

答えて

2

に時間コードコードを入力する必要があります

[self.delegate previousViewController:self itemToSend:@"From Previous VC"]; 
[self.navigationController popViewControllerAnimated:true]; 
あなただけの.mファイルにコードの下に追加する必要があり、あなたのCallViewViewControllerで

#import "CEPeoplePickerNavigationController.h" 

@interface ViewController()< CEPeoplePickerNavigationController Delegate> 

メソッドを追加し、以前のビューコントローラ

- (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 check http://stackoverflow.com/questions/42197175/unable-to-receive-callback-from-second-view-controller –

2

CallViewViewControllerからCEPeoplePickerNavigationControllerに移動しているときに、次のVCに進むためにsegueを押すか適用する何らかのナビゲーションが必要です。この後は

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

- あなたはインスタンス化を使用している場合

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

- あなたはセグエにを使用している場合

-

ただ、そこのコード行を追加しますそれが次のコントローラの代理人に応答します

EDIT

CEPeoplePickerNavigationControllerをクリアしてください。あなたは自分が以前のビューコントローラのイベントに移動するには、あなたの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 

上のコードの下にカスタム委任

書き込みを使用してデータを渡す必要があり、次の

@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

Im instantiateViewControllerWithIdentifierを使用して –

+0

これに応じて回答を編集する –

+0

よろしくお願いします...... –

関連する問題