objective-c - 如何从cocoa(objective-c)中的另一个委托(delegate)类调用TableView委托(delegate)函数

标签 objective-c xcode cocoa

当用户在 NSSearchField 上按 Enter 键后,我尝试将文本从 NSSearchField 添加到 NSTableView。我为 NSTableView 和 NSSearchField 创建了 2 个委托(delegate)。

我在 SearchFieldController (委托(delegate))中使用此代码:

-(void)controlTextDidEndEditing:(NSNotification *)notification
{
      // See if it was due to a return
      if ( [[[notification userInfo] objectForKey:@"NSTextMovement"] intValue] == NSReturnTextMovement )
      {
         NSArray *myarr = [NSArray arrayWithObjects:@"123" ,@"456" , @"789" , nil];

        [(TableViewController *) MySelf addObjects:myarr];
        NSLog(@"Return was pressed!");
     }
}

SearchFieldController.h:

#import <Foundation/Foundation.h>
#import "TableViewController.h"


@interface SearchFieldController : NSSearchField{
@public
    IBOutlet NSSearchField *searchField;
    BOOL isEnterKey;
    TableViewController *MySelf;
}

//@property (assign) BOOL isEnterKey;


@end

TableViewController.m(委托(delegate))中的这个函数用于添加对象:

- (void)addObjects:(NSArray *)Objects{
    for (NSString *file in Objects) {
        Item *item = [[Item alloc] init];
        item.name = file;
        [list addObject:item];
        [tableView reloadData];
        [item release];
    }
}

但是当我测试该应用程序时,什么也没有发生!我的 UITableView 中没有添加任何内容,并且我没有收到任何错误!知道我做错了什么吗?

最佳答案

您在向数组添加对象后忘记保留数组。

- (void)addObjects:(NSArray *)Objects{
    for (NSString *file in Objects) {
        Item *item = [[Item alloc] init];
        item.name = file;
        [list addObject:item];
        [list retain];
        [tableView reloadData];
        [item release];
    }
}

关于objective-c - 如何从cocoa(objective-c)中的另一个委托(delegate)类调用TableView委托(delegate)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15018274/

相关文章:

iPhone 应用程序随机崩溃,没有任何错误或堆栈跟踪

swift - 如何在 Xcode 中围绕一个点或 UIView 的中心旋转一个 Button?

objective-c - 无法在参数传递中将 'b2PolygonShape' 转换为 'objc_object*'

objective-c - 使用 Singleton 类的全局变量 NSMuteableArray

ios - UIImage 通过选择另一个 UIImage 的一部分

iphone - 释放 NSURLConnection 对象

xcode - @autoreleasepool没有ARC吗?

json - 使用 guard 语句从 json 解包到 AnyObject

objective-c - 将枚举转换为类层次结构

objective-c - 始终在 NSDocument 中附加文件扩展名