gpt4 book ai didi

ios - 无法从第二个 View Controller 接收回调

转载 作者:行者123 更新时间:2023-11-28 21:13:43 24 4
gpt4 key购买 nike

我有两个 View Controller 。我正在尝试将数据传递给之前的 View Controller

我的第二个 View Controller 中有以下代码 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

当用户点击完成按钮时,将执行以下代码

- (void)doneAction:(id)sender
{
[self.peoplePickerDelegate previousViewController:self itemToSend:@"From Previous VC"];
[self dismissViewControllerAnimated:YES completion:nil];
}

在我的第一个 View Controller 中,我在头文件中有以下接口(interface),当然我已经在我的第一个 View Controller 中实现了 previousViewController 方法,其中必须接收数据

@interface CallViewViewController : UIViewController<CEPeoplePickerNavigationControllerDelegate>
@end

当我从第一个 View Controller 移动到第二个 View Controller 时,我使用以下代码。

CEPeoplePickerNavigationController *nextVc = [[CEPeoplePickerNavigationController alloc] init];
nextVc.peoplePickerDelegate = self;

[self presentViewController:nextVc animated:YES completion:nil];

但是当用户从第二个 View Controller 单击完成按钮时,我在第一个 View Controller 中没有收到任何回调。为什么会这样?

我实现了如下接口(interface),

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

更新:

以下代码有效

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");
}

这就是我实例化 View Controller 的方式。

CEPeoplePickerNavigationController *nextVc = [[CEPeoplePickerNavigationController alloc] init];

nextVc.viewCrtrlDelegate = self;
[self presentViewController:nextVc animated:YES completion:nil];

最佳答案

您的代码似乎绝对正确,但要避开所有可能性。您能否检查您的完成按钮操作是否检查委托(delegate)方法是否在该类中可用?

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");
}

关于ios - 无法从第二个 View Controller 接收回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42197175/

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