Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

medasmx

macrumors member
Original poster
Nov 9, 2008
89
0
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
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 

SydneyDev

macrumors 6502
Sep 15, 2008
346
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.