gpt4 book ai didi

ios - 无法将数据传递给之前的 View Controller

转载 作者:行者123 更新时间:2023-11-28 18:50:29 25 4
gpt4 key购买 nike

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

我有两个 View Controller 如下

  1. 调用 View View Controller
  2. CEPeoplePickerNavigationController

这是我对CallViewViewController的接口(interface)定义

@interface CallViewViewController ()<CEPeoplePickerNavigationControllerDelegate>{

}

@property(nonatomic)CustomMessageClass * customMessageClassObject;

@end

在我的实现中,我当然实现了委托(delegate)方法

-(void)cePeoplePickerNavigationController:(CEPeoplePickerNavigationController *)peoplePicker didFinishPickingPeople:(NSArray *)peopleArg values:(NSDictionary *)valuesArg
{
NSLog(@"can populate the tableview");
}

这是我的第二个类的接口(interface)定义,

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

但是数据并没有传回之前的 View Controller 。为什么会这样?

更新

我尝试了以下代码将表单首先移动到第二个 View Controller ,

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

但它抛出以下错误,

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

最佳答案

您只需要使用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

在移动到上一个 View Controller 事件时,您需要将代码放在下面

[self.delegate previousViewController:self itemToSend:@"From Previous VC"];
[self.navigationController popViewControllerAnimated:true];

在您的 CallViewViewController 上,您只需在 .m 文件中添加以下代码

#import "CEPeoplePickerNavigationController.h"

@interface ViewController ()< CEPeoplePickerNavigationController Delegate>

在之前的 View Controller 上添加方法声明

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

确保当您导航到 CEPeoplePickerNavigationController 时,您需要将委托(delegate)分配给自己,如下所示

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

关于ios - 无法将数据传递给之前的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42175567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com