UIPickerView控件是比UIDatePicker控件更普通的Picker控件,UIDatePicker控件能夠理解成是從UIPickerView控件加工出來的專門進行日期選擇的控件。atom
UIPickerView控件的用法比UIDatePicker複雜一點。本文中的小例子將用UIPickerView控件作出兩種效果,第一個只有一個轉盤,第二個有兩個轉盤,但這兩個轉盤之間沒有依賴關係,也就是說改變其中一個轉盤中的選擇,不會對第二個轉盤產生影響。在下一篇文章會作一個轉盤之間有依賴關係的例子。code
下圖是咱們的效果圖:component
第一個UIPickerView控件能夠用來選擇Horse,Sheep,Pig,Dog,Cat,Chicken,Duck,Goose;第二個UIPickerView在第一個基礎上增長了一個轉盤。orm
閒話少說,接下來就開始。it
一、運行Xcode 4.2,新建一個Single View Application,名稱爲UIPickerView Test1,其餘設置以下圖:io
二、單擊ViewController.xib,而後拖一個Picker View控件到視圖上:class
而後再拖一個Button到Picker View下方,並修更名稱爲Select:import
三、在ViewController.h中爲Picker View控件建立Outlet映射,名稱爲myPickerView,而後爲Select按鈕建立Action映射,名稱爲buttonPressed,具體方法不說了,能夠參照上一篇文章。基礎
四、選中Picker View控件,打開Connections Inspector,找到delegate和datasource,從它們右邊的圓圈拉線到File’s Owner:sed
五、單擊ViewController.h,在其中添加代碼:
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource> @property (weak, nonatomic) IBOutlet UIPickerView *myPickerView; @property (strong, nonatomic) NSArray *myPickerData; - (IBAction)buttonPressed:(id)sender; @end
注意在@interface後面添加尖括號及其中內容,咱們將ViewController做爲Picker View的Delegate以及DataSource。
六、代碼添加:
6.1 單擊ViewController.m,在@implementation的下一行添加代碼:
@synthesize myPickerData;
6.2 找到buttonPressed方法,添加代碼以下:
- (IBAction)buttonPressed:(id)sender { NSInteger row = [myPickerView selectedRowInComponent:0]; NSString *selected = [myPickerData objectAtIndex:row]; NSString *msg = [[NSString alloc] initWithFormat: @"You selected %@!", selected]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello!" message:msg delegate:nil cancelButtonTitle:@"Yes, I Did." otherButtonTitles:nil]; [alert show]; }
6.3 找到viewDidLoad方法,在其中添加代碼:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSArray *array = [[NSArray alloc] initWithObjects:@"Horse", @"Sheep", @"Pig", @"Dog", @"Cat", @"Chicken", @"Duck", @"Goose", nil]; self.myPickerData = array; }
6.4 找到viewDidUnload方法,在其中添加代碼:
- (void)viewDidUnload { [self setMyPickerView:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; self.myPickerView = nil; self.myPickerData = nil; }
6.5 在@end前面添加代碼:
#pragma mark - #pragma mark Picker Data Source Methods - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [myPickerData count]; } #pragma mark Picker Delegate Methods - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [myPickerData objectAtIndex:row]; }
七、運行:
上面的例子只有一個轉盤,接下來咱們在此基礎上增長一個轉盤,第一個轉盤不變,第二個轉盤能夠選擇Tree,Flower,Grass,Fence,House,Table,Chair,Book,Swing。只要添加代碼就好了。
八、單擊ViewController.h,在@interface下一行添加代碼:
@property (strong, nonatomic) NSArray *myPickerData_2;
九、單擊ViewController.m,在其中添加代碼:
9.1 在@implementation的下一行添加代碼:
@synthesize myPickerData_2;
9.2 找到viewDidLoad方法,在其中添加代碼:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSArray *array = [[NSArray alloc] initWithObjects:@"Horse", @"Sheep", @"Pig", @"Dog", @"Cat", @"Chicken", @"Duck", @"Goose", nil]; self.myPickerData = array; NSArray *array_2 = [[NSArray alloc] initWithObjects:@"Tree", @"Flower", @"Grass", @"Fence", @"House", @"Table", @"Chair", @"Book",@"Swing" , nil]; self.myPickerData_2 = array_2; }
9.3 找到viewDidUnload方法,在其中追加代碼:
- (void)viewDidUnload { [self setMyPickerView:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; self.myPickerView = nil; self.myPickerData = nil; self.myPickerData_2 = nil; }
9.4 找到buttonPressed方法,修改代碼:
- (IBAction)buttonPressed:(id)sender { NSInteger row = [myPickerView selectedRowInComponent:0]; NSInteger row_2 = [myPickerView selectedRowInComponent:1]; NSString *selected = [myPickerData objectAtIndex:row]; NSString *selected_2 = [myPickerData_2 objectAtIndex:row_2]; NSString *msg = [[NSString alloc] initWithFormat: @"You selected %@ and %@!", selected, selected_2]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello!" message:msg delegate:nil cancelButtonTitle:@"Yes, I Did." otherButtonTitles:nil]; [alert show]; }
9.5 找到numberOfComponentsInPickerView方法,修改其返回值爲2:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 2; }
9.6 找到numberOfRowsInComponent方法,修改其中代碼:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if (component == 0) { return [myPickerData count]; } return [myPickerData_2 count]; }
9.7 找到下面的方法,修改代碼:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (component == 0) { return [myPickerData objectAtIndex:row]; } return [myPickerData_2 objectAtIndex:row]; }
十、運行: