无法将数据传递给前一个视图控制器

Posted

技术标签:

【中文标题】无法将数据传递给前一个视图控制器【英文标题】:Unable to pass data to the previous viewcontroller 【发布时间】:2017-02-11 11:57:21 【问题描述】:

我正在尝试实现委托设计模式。

我有两个视图控制器如下m

    CallViewViewController 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

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

 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”

【问题讨论】:

你的第一个视图控制器是否接受委托? no...第一个视图控制器委托没有被调用 我正在使用 instantiateViewControllerWithIdentifier 我猜@Saheb Roy 给出了完整的解决方案 它正在使应用程序崩溃 【参考方案1】:

您只需要使用 Custom Delegate

传递数据

在您的 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];

【讨论】:

请检查***.com/questions/42197175/…【参考方案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

【讨论】:

我正在使用 instantiateViewControllerWithIdentifier 相应地编辑答案 应用在 nextVC.peoplePickerDelegate = self; 处显示错误; 发布了什么错误?并且请检查语法,因为我在 Windows 机器上发布了这个答案,所以我无法访问 Xcode 来自动更正语法,因为某些字符可能有点偏离 我已经用我遇到的错误更新了问题

以上是关于无法将数据传递给前一个视图控制器的主要内容,如果未能解决你的问题,请参考以下文章

如何将数据传递给嵌套控制器(UIContainerView)?

如何将数据传递给其他视图? [关闭]

如何在将数据推送到导航堆栈之前将数据传递给视图控制器并加载它?

将数据传递给目标视图控制器[重复]

将数据传递给 viewController

CodeIgniter 将数据传递给视图