Folgendes Beispiel für den UIPickerview-Controller
.m-Dateien hier
#import "SingleComponentPickerViewController.h"
@implementation SingleComponentPickerViewController
@synthesize singlePicker;
@synthesize pickerData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
-(IBAction)buttonPressed1
{
NSInteger row = [singlePicker selectedRowInComponent:0];
NSString *selected = [pickerData objectAtIndex:row];
NSString *title = [[NSString alloc] initWithFormat:
@"you selected %@!", selected];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message : @"Thank you for choosing."
delegate:nil
cancelButtonTitle :@"Welcome"
otherButtonTitles :nil];
[alert show];
[alert release];
[title release];
}
- (void)viewDidLoad {
NSArray *array = [[NSArray alloc] initWithObjects:@"iPhone",@"iPad",@"iPod",@"iMac",@"Mac",
@"iBook",@"Safari",nil];
self.pickerData = array;
[array release];
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[singlePicker release];
[pickerData release];
[super dealloc];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [pickerData count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
return[pickerData objectAtIndex:row];
}
@end
und .h-Datei hier
#import <UIKit/UIKit.h>
@interface SingleComponentPickerViewController : UIViewController {
IBOutlet UIPickerView *singlePicker;
NSArray *pickerData;
}
@property(nonatomic , retain) UIPickerView *singlePicker;
@property(nonatomic , retain) NSArray *pickerData;
-(IBAction)buttonPressed1;
@end
glückliche Kodierung @samuel