Kiarash Kiani
Kiarash Kiani

Reputation: 45

How to call TableViewDelegate Function from another Delegate class in cocoa (objective-c)

I try to add text from NSSearchField to NSTableView after the user presses enter key on NSSearchField. I created 2 Delegates, for NSTableView and NSSearchField.

I use this code in 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

And this function in TableViewController.m (Delegate) for Adding objects:

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

but when I test the app, nothing happens! nothing is added to my UITableView and I don't get any error! Any idea what I'm doing wrong?

Upvotes: 2

Views: 231

Answers (1)

Baby Groot
Baby Groot

Reputation: 4279

You forgot to retain your array after adding object in it.

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

Upvotes: 1

Related Questions