2011-04-27 4 views

Respuesta

12

ejemplo escrito rápidamente sobre la base de una matriz con los valores deseados:

SampleAppDelegate.h

#import <Cocoa/Cocoa.h> 

@interface SampleAppDelegate : NSObject <NSApplicationDelegate> { 

    NSWindow * window; 
    NSArray * values; 

    IBOutlet NSSlider * theSlider; 
    IBOutlet NSTextField * theLabel; 

} 

- (IBAction)sliderChanged:(id)sender; 

@property (assign) IBOutlet NSWindow *window; 

@end 

SampleAppDelegate.h

#import "SampleAppDelegate.h" 

@implementation SampleAppDelegate 

@synthesize window; 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 

    values = [[NSArray alloc] initWithObjects:@"0",@"1",@"2",@"10",@"50",nil]; 

    [theSlider setNumberOfTickMarks:[values count]]; 
    [theSlider setMinValue:0]; 
    [theSlider setMaxValue:[values count]-1]; 
    [theSlider setAllowsTickMarkValuesOnly:YES]; 

    [theLabel setStringValue:[values objectAtIndex:0]]; 
    [theSlider setIntValue:0]; 


} 

- (IBAction)sliderChanged:(id)sender { 

    int current = lroundf([theSlider floatValue]); 
    [theLabel setStringValue:[values objectAtIndex:current]]; 

} 

@end 

Interface Builder:
- Añadir NSSlider (conectar IBOutlet/cone ect IBAction/activar la actualización continua)
- Añadir NSTextField (conectar IBOutlet)

Resultado:

enter image description here

+0

+1 Esta es la forma recomendada de hacerlo. –

+0

Muchas gracias, ese enfoque funcionó espléndidamente. –

+0

Anne ataca de nuevo. Buen código. :) –

Cuestiones relacionadas