How to assign a callback to NSPopupButton.Action property? ( or other means?)

TroyChardTroyChard CAMember ✭✭

Hi there,
I'm working on converting an app from Objective-C/Cocoa/Swift to Xamarin.Mac.

I've got a line of code here that wants to assign a selector to an NSPopupButton.Action property.

Is there a way of assigning callbacks to NSPopupButtons (dropdowns) in Xamarin.Mac? (either via this property or any other means, but without deriving the class?)
eg: Trying to find Xamarin Equivalent to something like this:

MyViewController.MyPopUpButton.action = #selector(self.doStuffWhenSelectionChanges(sender:))

Best Answer

Answers

  • TroyChardTroyChard CAMember ✭✭
    edited November 2017

    Of course seconds after hitting the post button I find the SelectionChanged event handler. Hopefully this is everything I need.

    Edit: Ah, I spoke too soon. Trying to use SelectionChanged on an NSPopupButton is giving me this:

    Error CS1656: Cannot assign to SelectionChanged' because it is a method group' (CS1656)

    Anyone know how to respond to an NSPopupButton.SelectionChanged event.

  • TroyChardTroyChard CAMember ✭✭

    I discovered a way to do it, but by using an NSComboBox instead of NSPopupButton

                TestCombo.Add(new NSString("Alpha"));
                TestCombo.Add(new NSString("Bravo"));
                TestCombo.Add(new NSString("Charlie"));
    
                TestCombo.SelectItem(0);
    
                TestCombo.SelectionChanged += (object sender, EventArgs e) =>
                {
                    PopupLabel.StringValue = $"Selected Index is {(int)TestCombo.SelectedIndex}";
                };
    

    (Thanks to Judef's reply in the discussion here )

    I'd still like to see if/how an NSPopUpButton can respond to dynamically added items being selected. But for now, by using an NSComboBox, this event handler gets invoked when the selection changes.

  • TroyChardTroyChard CAMember ✭✭
    edited December 2017

    edit Thanks JGoldberger! I just confirmed that this works.

    I assumed the Activated event was triggered when the NSPopupButton is clicked on. Is the btn.SelectedItem.Title in the event handler above the item that the user has chosen after a selection has been made? (and not just indicating that the NSPopUpButton has been clicked on? )

    (oh, and the SelectionChanged bit was coming from the Xamarin docs here that say

    Responding to Events
    Menu Controls provide the following events to respond to user interaction:

    SelectionChanged - Is called when the user has selected a value from the list.

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai
    edited December 2017

    Ah, I see... yes that is a bit confusing, but an NSPopupButton is derived from NSButton not NSMenu so the APi is different, and for an NSButton you use the Activated event. Unfortunately the section on NSPopupButton only shows how to add an event handler in the storyboard using a target/action, but does not show the event to subscribe to when not setting it up in a storyboard and using code only.

    And yes, the btn.SelectedItem.Title is the title for the item that the user just selected.

    Also, I just realized that you might want to try passing in false instead of true for the last parameter when creating the button here:

    NSPopUpButton popupBtn = new NSPopUpButton(new CGRect(20, View.Frame.Height - 50, 200, 30), true);
    

    In both cases the event handler was fired, but in one case the title of the button did not reflect the selected item after a new item is selected. That last parameter determines whether you get a Popup Button or a pull down list.

Sign In or Register to comment.