new program NSPopUpButton

Discussion in 'Mac Programming' started by medasmx, Dec 14, 2008.

  1. medasmx macrumors member

    Nov 9, 2008
    I started from scratch to make a new popupbutton program. Below is the code.

    #import <Cocoa/Cocoa.h>

    @interface firsttrypopupasm : NSObject {
    IBOutlet NSTextField*output;
    IBOutlet NSPopUpButton*adamPopUp;


    #import "firsttrypopupasm.h"

    @implementation firsttrypopupasm

    adamPopUp=[[NSPopUpButton alloc]initWithFrame:NSMakeRect(0.0, 00.0, 75.0, 14.0) pullsDown:NO];
    [adamPopUp addItemsWithTitles:[NSArray arrayWtihObjects:mad:"show one",@"show two",@"show three",nil]];
    if ([[adamPopUp selectedItem] isEqualToString:mad:"show one"])
    [output setStringValue:mad:"one"];
    if ([[adamPopUp selectedItem] isEqualToString:mad:"show two"])
    [output setStringValue:mad:"two"];
    if ([[adamPopUp selectedItem] isEqualToString:mad:"show three"])
    [output setStringValue:mad:"three"];


    The idea with this program is that you have a popupbutton, with three elements, and a text field. When you scroll between elements in the popupbutton -- show one, show two, show three -- one, two, and three appear in the textfield. The program runs without errors, but when it is run, when scrolling between elements in the popupbutton, nothing shows up in the textfield. Appreciate any ideas. Thanks.

  2. kainjow Moderator emeritus


    Jun 15, 2000
    Is that all the code? Is the popup button connected and created in the nib? If so, then you don't need to recreate it dynamically, and you don't need to set its items in code (unless they're dynamic). Just do that in the nib (which you may already be doing). You do need to set the target/action of the popup menu to the adamPopUp: method also for it to be called when the selected item changes.
  3. SydneyDev macrumors 6502

    Sep 15, 2008
    Another way to do this is "bindings" where you can tell it in Interface Builder to keep the text field and popup synchronized without any code needed. But best to learn target/action first if you are just starting.

Share This Page