# Setting the items of anÃ?NSPopUpButton programmatically

**URL:** <https://www.ruby-forum.com/t/setting-the-items-of-ana-nspopupbutton-programmatically/101011>\
**Category:** Ruby\
**Created:** [February 4, 2006, 10:25am UTC](https://www.ruby-forum.com/t/setting-the-items-of-ana-nspopupbutton-programmatically/101011 "2006-02-04T10:25:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 4, 2006, 10:25am UTC](https://www.ruby-forum.com/t/setting-the-items-of-ana-nspopupbutton-programmatically/101011/1 "2006-02-04T10:25:00Z")

</div>

i’m starting within RubyCocoa, although i’ve been using Cocoa-Java, i  
want to setup the items of an NSPopUpButton programmatically.

the related named ib\_outlets is called “:choixTheme”

if i list all the associated methods by :

```
            ctmethods=@choixTheme.methods
            ctmethods.each { |m| p "choixTheme #{m}"}

```

i never get even the “titleOfSelectedItem” nor “indexOfSelectedItem”

why ?

if i try :

@choixTheme.addItem(“Unbelievable”)  
i get an error :  
/Library/Frameworks/RubyCocoa.framework/Versions/A/Resources/ruby/osx/ob  
jc/oc\_wrapper.rb:17:in `NSApplicationMain’: NSApplicationMain -  
RBException\_OSX::OCMessageSendException - NSPopUpButton#addItem: -  
methodSignature is nil. (OSX::OCException)

then, what’s the correct way to setup all the items of a NSPopUpButton  
programmatically ?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 10, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/setting-the-items-of-ana-nspopupbutton-programmatically/101011/2 "2006-02-10T23:37:50Z")

</div>

On Feb 4, 2006, at 1:23, Une bévue wrote:

> if i try :
> 
> @choixTheme.addItem(“Unbelievable”)  
> i get an error :

That’d be because (as far as I can tell) it doesn’t have an “addItem”  
method. 🙂

> then, what’s the correct way to setup all the items of a NSPopUpButton  
> programmatically ?

```
	itemArray = ["Unbelievable","Believable"]
	@choixTheme.removeAllItems
	@choixTheme.addItemsWithTitles(itemArray)
	@choixTheme.selectItemWithTitle(itemArray[0])

```

I “removeAllItems” because there are some in it when I create it in  
InterfaceBuilder, and I just didn’t care to bother figuring out how to  
place a completely empty pop-up button. 🙂 Also, I sometimes refresh  
it, and just emptying it and refilling from scratch is easier than  
modifying what’s there.

I’m sure there are other ways to do this as well.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 11, 2006, 10:48am UTC](https://www.ruby-forum.com/t/setting-the-items-of-ana-nspopupbutton-programmatically/101011/3 "2006-02-11T10:48:33Z")

</div>

Dave H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I “removeAllItems” because there are some in it when I create it in  
> InterfaceBuilder, and I just didn’t care to bother figuring out how to  
> place a completely empty pop-up button. 🙂 Also, I sometimes refresh  
> it, and just emptying it and refilling from scratch is easier than  
> modifying what’s there.
> 
> I’m sure there are other ways to do this as well.

fine, thanks, i didn’t notice the “s” in “removeAllItems”, i have to  
learn how to read the classes/methods browser (AppKiDo) 😉

because i’m using a loop :  
@choixTheme.removeAllItems

```
@prefs.themes_list.each { |theme|
    @choixTheme.addItemWithTitle theme.label
}

```
