1

I have an IB NSPopUpButton with a list of items and a number of NSTextFields.

The items from the popup are used to populate the text fields (ingredients) until a recipe is complete.

I would like to reset the popup title after each item is used but cannot find the code and syntax to do this.

I'd also like to be able to click on any of the text fields after selecting an ingredient to drop it there. Drag and drop from the popup would be ideal but I cannot find a simple way to do either so am currently using another button adjacent to each text field to initiate the drop. This is neither elegant nor ideal. Hopefully someone can propose better solutions.

My code currently looks like this

var ItemLabel: String = ""

@IBAction func Ingredients(_ sender: NSPopUpButton){

    ItemLabel = sender.titleOfSelectedItem ?? "Nil"
}
@IBOutlet weak var Ingredient1: NSTextField!

@IBAction func AddIngredient1(_ sender: NSButton){ // button next to text field

    Ingredient1Label.stringValue = ItemLabel
    // Here I need to reset the popup title

}
Willeke
  • 14,578
  • 4
  • 19
  • 47
Terence K
  • 41
  • 5
  • You can's set the title of a popup button, instead set the selected item. – Willeke Feb 08 '19 at 14:40
  • Consider a list (table view) instead of a popup button. – Willeke Feb 08 '19 at 14:43
  • The first item of the list is 'Ingredients' so what is the syntax for setting the popup to this item? – Terence K Feb 08 '19 at 15:25
  • As you can surmise I'm new to this . Tx. I'll look at table view too. – Terence K Feb 08 '19 at 15:26
  • See [NSPopUpButton](https://developer.apple.com/documentation/appkit/nspopupbutton), "Setting the current selection". – Willeke Feb 08 '19 at 15:34
  • Thank you. Even finding the documentation is difficult to start with. – Terence K Feb 08 '19 at 15:56
  • I still don't get there. How would I address the correct object from within the func AddIngredient1 in the code above? Think I need the whole code as an example. – Terence K Feb 08 '19 at 16:17
  • Which object is "the correct object"? Please describe how the gui should work. Do you use the popup button as menu to execute commands? – Willeke Feb 08 '19 at 21:13
  • After a great deal of searching I found that the answer to reset the popup item after using it in another function is to simply add another outlet which connects from the text field function to the popup item. Then it is addressable from within the text field function. The lines added then were, @IBOutlet weak var Ingredients: NSPopUpButton! and to the text field function Ingredients.selectItem(at: 0), Item 0 being the first item which I incorrectly refered to as the title. – Terence K Feb 09 '19 at 15:16
  • Take a look at a popup button in pull-down mode, the first item is the title. – Willeke Feb 09 '19 at 17:46

0 Answers0