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

标签 iphone ios uitableview delegates

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

基本上我有一个父 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/

相关文章:

ios - 更改 UIAlertView 中 UITextField 的字体大小

ios - 保存时随机核心数据 EXC_BAD_ACCESS

iphone - 如何解决 uitableview 中对象的释放问题?

ios - 在自定义UIview中设置UItableview的delegate和数据源

iphone - Facebook iOS SDK 无缘无故给出 FBErrorCategoryUserCancelled?

iPhone CSS 问题

ios - Alamofire json 请求 Swift 3

ios - 通过点击状态栏滚动到顶部 TableView 不起作用(有很多 subview )SWIFT

objective-c - iOS:UIView 的简单物理

ios - swift 2.2 : Class 'XX' has no initializers