我有兩個視圖控制器。我試着將數據傳遞到上一視圖 - 控制無法接收來自第二視圖控制器的回調
我已經在我的第二個視圖控制器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
當用戶點擊完成按鈕下面的代碼,下面的代碼將exectied
- (void)doneAction:(id)sender
{
[self.peoplePickerDelegate previousViewController:self itemToSend:@"From Previous VC"];
[self dismissViewControllerAnimated:YES completion:nil];
}
在我的第一個視圖控制器中,我在頭文件中有以下接口,並且我在我的第一個視圖控制器中實現了previousViewController方法,其中必須接收數據
@interface CallViewViewController : UIViewController<CEPeoplePickerNavigationControllerDelegate>
@end
當我從第一個視圖控制器移動到第二個視圖控制器時,使用下面的代碼。
CEPeoplePickerNavigationController *nextVc = [[CEPeoplePickerNavigationController alloc] init];
nextVc.peoplePickerDelegate = self;
[self presentViewController:nextVc animated:YES completion:nil];
但是,當用戶從第二視圖控制器點擊完成按鈕,Im不接收在我的第一視圖控制器的任何回調。爲什麼這樣?
我已經實現了界面,如下圖所示
- (void)previousViewController:(CEPeoplePickerNavigationController *)controller itemToSend:(NSString *)item
{
NSLog(@"from CEPeoplePickerNavigationController %@",item);
}
UPDATE:
下面的代碼工作
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");
}
但是,如果我嘗試通過以下方式,它不工作
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");
}
這是我如何實例化視圖控制器。
CEPeoplePickerNavigationController *nextVc = [[CEPeoplePickerNavigationController alloc] init];
nextVc.viewCrtrlDelegate = self;
[self presentViewController:nextVc animated:YES completion:nil];
方法執行與否 –
顯示完成按鈕的按鈕操作。 –
我已經附上完成的按鈕動作 –