1

I have an NSTableView and in one cell I have an NSPopUpButton... ok. I want to set the selected item in the pop up button based on a value but no matter what I do I can't seem to set which menu item gets selected...

What I need is if p.status is "Approved" then set the menu item in "statusMenu" to "Approved" but it just doesn't work. I have the bindings set correctly (I think) the pop up button is bound to "statusMenu" and if I setup a breakpoint I can view the menu correctly... I can also retrieve the value of the selected menu item and it logs correctly to "Approved" but the actual popup button doesn't change...

here's the code:

NSTableCellView *cell = [table_view makeViewWithIdentifier:identifier owner:self];
if([identifier isEqualToString:@"status"]){
    if(p){

        for (NSView *subview in cell.subviews) {
            if (![subview isKindOfClass:[NSButton class]]) continue;

            if ([subview.identifier isEqualToString:@"statusMenu"]){
                if ([p.status isEqualToString:@"Approved"]){
                    [(NSButton *)subview setStringValue:@"Approved"];
                    [statusMenu selectItemWithTitle:@"Approved"];
                    NSLog(@"selected: %@", [statusMenu titleOfSelectedItem]); //the Log is "Approved" but the pop up button doesn't change...!!
                }
            }
        }
    }
}

Here's my header:

    @interface ProposalTableViewController : NSObject<NSTableViewDataSource, NSTableViewDelegate, QLPreviewPanelDelegate, QLPreviewPanelDataSource>{
@public  
//A bunch of other stuff that's not relevlant     
    IBOutlet NSPopUpButton *statusMenu;
    }

Here are my bindings...

Bindings screengrab

julian
  • 157
  • 1
  • 12
  • Are you using bindings or do you set the values in code? Are `statusMenu` and `(NSButton *)subview` two controls? – Willeke Feb 24 '17 at 10:38
  • I am using bindings and the button is created in IB and the menu items are created there. It's setup in the header as: IBOutlet NSPopUpButton *statusMenu I haven't tried creating it in code but I suspect that's what I'll have to do- ugh. With (NSButton *)subview I was just throwing something at the wall to see if it sticks. – julian Feb 24 '17 at 12:25
  • Do you use bindings in the Bindings inspector or "bindings" in the Connections inspector? – Willeke Feb 24 '17 at 12:29
  • Connections inspector... I'm guessing I ought to try the bindings inspector? Am I walking into a "face palm" situation ;) – julian Feb 24 '17 at 13:18
  • Ignore the bindings inspector and call the connections "connections" to avoid confusion. Of which object is `statusMenu` an outlet? It should be an outlet of the cell (a subclass of `NSTableCellView`), like `imageView` and `textField`. – Willeke Feb 24 '17 at 13:42
  • I think you lost me here... I *think* I have `statusMenu` an outlet of the cell but I'm not sure how to verify... – julian Feb 24 '17 at 14:37
  • So it works if I create the button programmatically but that's not what I want to do as it will complicate everything way too much. I just don't get why I can't set the button...grr. – julian Feb 24 '17 at 16:25
  • You created an outlet from the viewcontroller to the popup button, it is always pointing to the same popup button. What you want is an outlet from each cell view which points to the popup button inside this cell view. It's like this question [Create a custom cell in a NSTableView](http://stackoverflow.com/questions/28112787/create-a-custom-cell-in-a-nstableview/28130267#28130267) but instead of an extra textfield and image view, you create a popup button. – Willeke Feb 24 '17 at 17:13

0 Answers0