当用户在 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/