new program NSPopUpButton

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

  1. medasmx macrumors member

    Joined:
    Nov 9, 2008
    #1
    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;
    }
    -(IBAction)adamPopUp:(NSPopUpButton*)sender;

    @end

    #import "firsttrypopupasm.h"


    @implementation firsttrypopupasm

    -(IBAction)adamPopUp:(NSPopUpButton*)sender;
    {
    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"];
    }

    @end

    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.

    Adam
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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

    Joined:
    Sep 15, 2008
    #3
    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