pickerViewController.h
#import <UIKit/UIKit.h> @interface pickerViewController : UIViewController{ UIPickerView *myPicker; NSArray *pickerData; } @property (nonatomic, retain) IBOutlet UIPickerView *myPicker; @property (nonatomic, retain) IBOutlet NSArray *pickerData; -(IBAction) buttonPressed; @end
pickerViewController.m
#import "pickerViewController.h" @implementation pickerViewController @synthesize myPicker; @synthesize pickerData; -(IBAction) buttonPressed{ NSInteger row = [myPicker 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:@"Cansel" otherButtonTitles:@"Buy Now", nil]; [alert show]; [alert release]; [title release]; } -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1; } -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return[pickerData count]; } -(NSInteger *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ return [pickerData objectAtIndex:row]; } // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { NSArray *array = [[NSArray alloc] initWithObjects:@"Cheetah", @"Puma", @"Jaguar", @"Panther", @"Tiger", @"Leopard", @"Snow Leopard", @"Lion", nil]; self.pickerData = array; [super viewDidLoad]; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; self.myPicker = nil; self.pickerData = nil; [super viewDidUnload]; } - (void)dealloc { [super dealloc]; } @end
参考
學寫iPhone程式 第94堂 - 開始 Single Picker
http://www.youtube.com/watch?v=OqFP3Rt8L8I&feature=related&hd=1
YouTube - 學寫iPhone程式 第95堂 - 繼續Single Picker
http://www.youtube.com/watch?v=IcVG1EbJmaI&feature=related&hd=1
YouTube - 學寫iPhone程式 第96堂 - 完成Single Picker
http://www.youtube.com/watch?v=eSrSCpd21Kk&feature=related&hd=1
詳解 Objective-C 2.0 改訂版: 荻原 剛志 | iPhoneプログラミングUIKit詳解リファレンス: 所 友太, 京セラコミュニケーションシステム株式会社 | iPhone SDK アプリケーション開発ガイド: Jonathan Zdziarski, 近藤 誠 (監訳), 武舎 広幸, 武舎 るみ |
0 件のコメント:
コメントを投稿