Question

I have a NSTableView where items can be added and deleted. Once items have been added to the table, I would like those items to also show as items for an NSPopUpButton. I tried the addItemsWithTitles: method but it gives me an error.

#import "TableController.h"
#import "Favorites.h"


@interface TableController ()

@property NSMutableArray *array;
@property (weak) IBOutlet NSTableView *tableView;
@property (weak) IBOutlet NSPopUpButton *popButton;

@end


@implementation TableController

- (id)init {
    self = [super init];
    if (self) {
        _array = [[NSMutableArray alloc] init];
    }
    return self;
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    return [_array count];
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    Favorites *fav = [_array objectAtIndex:row];
    NSString *ident = [tableColumn identifier];
    return [fav valueForKey:ident];
}

- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    Favorites *fav = [_array objectAtIndex:row];
    NSString *ident = [tableColumn identifier];
    [fav setValue:object forKey:ident];
}

- (IBAction)add:(id)sender {
    [_array addObject:[[Favorites alloc] init]];
    [_tableView reloadData];
    [_popButton addItemsWithTitles:_array];
}

-(IBAction)delete:(id)sender {
    NSInteger row = [_tableView selectedRow];
    [_tableView abortEditing];
    if (row != -1) {
        [_array removeObjectAtIndex:row];
    }
    [_tableView reloadData];
}

@end

So I tried logging the objectAtIndex:0 for the array and didn't get a string but received some numbers instead:

Array string is <Favorites: 0x10013e820>

And also for reference my Favorites class is

#import "Favorites.h"

@interface Favorites ()

@property (copy) NSString *location;

@end


@implementation Favorites

- (id)init {
    self = [super init];
    if (self) {
        _location = @"City, State or ZIP";
    }
    return self;
}

@end
Was it helpful?

Solution

Your array has instances of your class, Favorites, and in your addItemsWithTiles: method you are adding those instances, not some string property of those instances, which is what I presume you want. If you use [_array valueForKey:@"whateverKeyHasYourStrings"] as the argument to addItemsWithTitles: instead of just _array, I think it will work ok.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top