gpt4 book ai didi

iphone - 为什么我的委托(delegate)方法没有被调用?

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

这可能很简单,但我卡住了!

基本上我有一个父 View Controller 和一个 subview Controller ,我正在尝试将数据从 subview 传递给父 View 。

//子VC接口(interface)

@protocol ANSearchGetawayFilterDelegate
-(void)selectedCell:(NSString *)cellTitle;
@end

@interface ANSearchGetawayFilterViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>
{
NSString* cellTitle;
}
@property (nonatomic, assign) id<ANSearchGetawayFilterDelegate> delegate;

@end

//子VC实现

@implementation ANSearchGetawayFilterViewController
@synthesize delegate = _delegate;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
cellTitle = selectedCell.textLabel.text;
[[self delegate] selectedCell:cellTitle];

[self dismissModalViewControllerAnimated:YES];
}

//父VC接口(interface)

#import "ANSearchGetawayFilterViewController.h"

@interface ANGetawayFilterViewController : UIViewController <ANSearchGetawayFilterDelegate>
{
NSString* _cellText;
}

//父VC实现

   - (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
ANSearchGetawayFilterViewController *search = [[ANSearchGetawayFilterViewController alloc] init];
search.delegate = self;
}
return self;
}

//delegate method

-(void)selectedCell:(NSString *)cellTitle
{
_cellText = cellTitle;
NSLog(@"cell text %@", _cellText);
}

永远不会调用委托(delegate)方法,什么时候 NSLog 和 _cellText 其他地方出现为空...我做错了什么?谢谢!

最佳答案

您很可能会在展示 ANSearchGetawayFilterViewController 实例时创建它,而不是在其上配置委托(delegate)。

当你打电话

ANSearchGetawayFilterViewController *search = [[ANSearchGetawayFilterViewController alloc] init];
search.delegate = self;

您创建了一个 ANSearchGetawayFilterViewController 实例,然后正确设置了委托(delegate),但是您从未将这个 ANSearchGetawayFilterViewController 实例存储在任何地方。所以稍后当你来展示它时,你会再次打电话

ANSearchGetawayFilterViewController *search = [[ANSearchGetawayFilterViewController alloc] init];

这给了你一个完全不同的实例,然后你需要重新配置它。例如

ANSearchGetawayFilterViewController *search = [[ANSearchGetawayFilterViewController alloc] init];
ANSearchGetawayFilterViewController *search1 = [[ANSearchGetawayFilterViewController alloc] init];

NSLog(@"%d", search1 == search);

#=> 0

要修复更新您的代码

- (BOOL)textFieldShouldBeginEditing:(UITextField*)textField;
{
BOOL shouldBeginEditing = YES;
NSLog(@"text field should begin editing");

ANSearchGetawayFilterViewController *myANSearchGetawayFilterViewController = [[[ANSearchGetawayFilterViewController alloc] init] autorelease];
myANSearchGetawayFilterViewController.delegate = self; // <--- configure the delegate

[self presentModalViewController:myANSearchGetawayFilterViewController animated:YES];
[self closeAllPickers];
return shouldBeginEditing;
}

我不会将它设为 ivar,因为您可能会暂时显示此 viewController 只是为了选择一些数据然后删除它,因此丢弃它并每次创建一个新数据可能是安全的。

关于iphone - 为什么我的委托(delegate)方法没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10807641/

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