如何从cocoa中的另一个Delegate类调用TableViewDelegate函数(objective-c)

时间:2013-02-22 06:33:41

标签: objective-c xcode cocoa

用户按下NSSearchField上的回车键后,我尝试将NSSearchField中的文字添加到NSTableView。我创建了2个代表,用于NSTableView和NSSearchField

我在SearchFieldController(代表)中使用此代码:

-(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没有添加任何内容,我也没有收到任何错误!知道我做错了吗?

1 个答案:

答案 0 :(得分:1)

在其中添加对象后,您忘记保留数组。

- (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];
    }
}