UIPickerView控件是比UIDatePicker控件更普通的Picker控件,UIDatePicker控件可以理解成是從UIPickerView控件加工出來的專門進(jìn)行日期選擇的控件。
UIPickerView控件的用法比UIDatePicker復(fù)雜一點(diǎn)。本文中的小例子將用UIPickerView控件做出兩種效果,第一個(gè)只有一個(gè)轉(zhuǎn)盤,第二個(gè)有兩個(gè)轉(zhuǎn)盤,但這兩個(gè)轉(zhuǎn)盤之間沒有依賴關(guān)系,也就是說改變其中一個(gè)轉(zhuǎn)盤中的選擇,不會(huì)對(duì)第二個(gè)轉(zhuǎn)盤產(chǎn)生影響。在下一篇文章會(huì)做一個(gè)轉(zhuǎn)盤之間有依賴關(guān)系的例子。
下圖是我們的效果圖:
第一個(gè)UIPickerView控件可以用來選擇Horse,Sheep,Pig,Dog,Cat,Chicken,Duck,Goose;第二個(gè)UIPickerView在第一個(gè)基礎(chǔ)上增加了一個(gè)轉(zhuǎn)盤。
閑話少說,接下來就開始。
1、運(yùn)行Xcode,新建一個(gè)Single View Application,名稱為UIPickerView Test1,其他設(shè)置如下圖:
2、單擊ViewController.xib,然后拖一個(gè)Picker View控件到視圖上:
然后再拖一個(gè)Button到Picker View下方,并修改名稱為Select:
3、在ViewController.h中為Picker View控件創(chuàng)建Outlet映射,名稱為myPickerView,然后為Select按鈕創(chuàng)建Action映射,名稱為buttonPressed,具體方法不說了,可以參照上一篇文章。
4、選中Picker View控件,打開Connections Inspector,找到delegate和datasource,從它們右邊的圓圈拉線到File's Owner:
5、單擊ViewController.h,在其中添加代碼:
@interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIPickerView *myPickerView;
@property (strong, nonatomic) NSArray *myPickerData;
- (IBAction)buttonPressed:(id)sender;
@end
6、代碼添加:
6.1 單擊ViewController.m,在@implementation的下一行添加代碼:
- (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];
}
上面的例子只有一個(gè)轉(zhuǎn)盤,接下來我們?cè)诖嘶A(chǔ)上增加一個(gè)轉(zhuǎn)盤,第一個(gè)轉(zhuǎn)盤不變,第二個(gè)轉(zhuǎn)盤可以選擇Tree,F(xiàn)lower,Grass,F(xiàn)ence,House,Table,Chair,Book,Swing。只要添加代碼就行了。
8、單擊ViewController.h,在@interface下一行添加代碼:
9.1 在@implementation的下一行添加代碼:
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];
}
進(jìn)階實(shí)例
下面要用UIPickerView控件做出這樣的效果:它有兩個(gè)轉(zhuǎn)盤(Component),當(dāng)左邊的轉(zhuǎn)盤改變了選擇值,右邊轉(zhuǎn)盤所有的選項(xiàng)都改變。如下圖所示:
為了達(dá)到這樣的效果,還是先要?jiǎng)?chuàng)建兩個(gè)NSArray對(duì)象,每個(gè)轉(zhuǎn)盤對(duì)應(yīng)一個(gè)。然后創(chuàng)建一個(gè)NSDictionary對(duì)象。我們可以想象出數(shù)據(jù)是樹形的,NSDictionary可以看成是一個(gè)有兩列的表格,第一列存儲(chǔ)的是關(guān)鍵字,每個(gè)關(guān)鍵字對(duì)應(yīng)一個(gè)NSArray對(duì)象,這些NSArray數(shù)組中存儲(chǔ)的是一系列的NSString對(duì)象。
在這個(gè)例子中,第一例存儲(chǔ)的是一些省份,第二列存儲(chǔ)的是省份對(duì)應(yīng)的地級(jí)市。
其實(shí)實(shí)現(xiàn)的方法跟上篇文章中的差不多,唯一不同的是要實(shí)現(xiàn):改變左邊轉(zhuǎn)盤的選項(xiàng),右邊轉(zhuǎn)盤內(nèi)容發(fā)生相應(yīng)的變化。這個(gè)功能要用到的函數(shù)我們上次也使用到了。
這次,我們先把要用到的代碼寫好,然后再用Interface Builder創(chuàng)建控件、實(shí)現(xiàn)映射等。
1、運(yùn)行Xcode 4.2,新建一個(gè)Single View Application,名稱為UIPickerView Test2:
2、創(chuàng)建數(shù)據(jù)。我們用到的數(shù)據(jù)如下:
在前邊的文章中曾經(jīng)提到過plist文件,現(xiàn)在,我們就要用plist文件存儲(chǔ)以上數(shù)據(jù)。為此,選擇File ― New ― New File,在打開的窗口中,左邊選擇iOS中的Resource,右邊選擇Property List:
單擊Next,在打開的窗口中,Save As中輸入名稱provinceCities,Group選擇Supporting Files:
單擊Create,就創(chuàng)建了provinceCities.plist。然后往其中添加數(shù)據(jù),如下圖所示:
3、單擊ViewController.h,向其中添加代碼:
#define kProvinceComponent 0
#define kCityComponent 1
@interface ViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
@property (strong, nonatomic) IBOutlet UIPickerView *picker;
@property (strong, nonatomic) NSDictionary *provinceCities;
@property (strong, nonatomic) NSArray *provinces;
@property (strong, nonatomic) NSArray *cities;
- (IBAction)buttonPressed;
@end
4.1 在@implementation下一行添加代碼:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == kProvinceComponent) {
return [self.provinces count];
}
return [self.cities count];
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == kProvinceComponent) {
return [self.provinces objectAtIndex:row];
}
return [self.cities objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == kProvinceComponent) {
NSString *selectedState = [self.provinces objectAtIndex:row];
NSArray *array = [provinceCities objectForKey:selectedState];
self.cities = array;
[picker selectRow:0 inComponent:kCityComponent animated:YES];
[picker reloadComponent:kCityComponent];
}
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
if (component == kCityComponent) {
return 150;
}
return 140;
}
最后一個(gè)方法
代碼部分結(jié)束,接下來是使用Interface Builder添加控件、創(chuàng)建映射。
5、單擊ViewController.xib,往其中添加一個(gè)UIPickerView控件和一個(gè)Button,按鈕的名稱改為“選擇”,具體方法參照前面一
接下來要做的就是拉幾條線。
6、選中新添加的UIPickerView控件,按住Control,拖到File's Owner圖標(biāo),在彈出菜單選擇delegate和dataSource:
打開Assistant Editor,確保其中打開的是ViewController.h,然后從picker屬性前邊的小圓圈拉線到UIPickerView控件:
同樣,從buttonPressed方法前邊的小圓圈拉線到“選擇”按鈕。
7、運(yùn)行:
新聞熱點(diǎn)
疑難解答
圖片精選