picker控件詳解與使用,(實現省市的二級聯動)
第一步:新建一個單視圖(single view)的工程,

命名為pickerTest,不要勾選下面兩個選項,第一個是新版本里面的,第二個是單元測試,現在用不着。

點擊next ->creat之后,打開工具欄:

在下面的控件工具欄中往視圖上拖放一個Picker View控件,一個UIButton控件,一個UILable控件,(所有在前端可以看得見的控件都繼承自UIView)

修改UIButton的title屬性,設置為click 然后點擊分欄按鈕,為控件連線(這種方式相對於手動去寫,要快速很多)。

依次為UILable 和 Picker View 控件添加插座變量,並且為UIButton添加Action

右擊picker控件,將 Datesource和delegate連線至File's Owner, 設置它的數據源和代理。

在 cidpViewController.h文件中添加幾個變量和遵循 UIPickerViewDelegate 協議,這樣就可以用UIPickerViewDelegate里面的幾個方法了 。
@interface cidpViewController : UIViewController<UIPickerViewDelegate>

#import <UIKit/UIKit.h> @interface cidpViewController : UIViewController<UIPickerViewDelegate> { UILabel *lblTitle; UIPickerView *picker; NSString* strProvince; NSString* strCity; NSMutableArray* aProvince; NSMutableArray* aCity; NSArray* tempArray; } @property (strong, nonatomic) IBOutlet UIPickerView *picker; @property (strong, nonatomic) IBOutlet UILabel *lblTitle; - (IBAction)btnClick:(id)sender; @end
#import "cidpViewController.h" @implementation cidpViewController @synthesize picker; @synthesize lblTitle; - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; //初始哈5個省份 aProvince = [[NSMutableArray alloc] initWithObjects:@"北京",@"甘肅",@"山西",@"湖北",@"廣州", nil]; //初始化5個數組 ,分別初始化為5個省的城市 NSArray* array1 = [NSArray arrayWithObjects:@"海淀",@"昌平",@"朝陽",@"西城",@"豐台", nil]; NSArray* array2 = [NSArray arrayWithObjects:@"蘭州",@"白銀",@"張掖",@"隴西",@"天水", nil]; NSArray* array3 = [NSArray arrayWithObjects:@"太原",@"大同",@"運城",@"晉城",@"五台山", nil]; NSArray* array4 = [NSArray arrayWithObjects:@"武漢",@"荊州",@"襄陽",@"贛州", nil]; NSArray* array5 = [NSArray arrayWithObjects:@"廣州",@"佛山",@"尖沙咀",@"中山", nil]; aCity = [[NSMutableArray alloc] initWithObjects:array1,array2,array3,array4,array5, nil]; //tempArray 用來初始化第二個 ,並且引用一次,避免提前釋放,內存出錯 tempArray = [array1 retain]; // 用來第一次顯示UIlable,負責的話就會顯示 null,null strProvince = [aProvince objectAtIndex:0]; strCity = [tempArray objectAtIndex:0]; [aProvince release]; [aCity release]; } -(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{ //返回2 表示將有兩個component(滾輪) return 2; } -(NSInteger)pickerView:(UIPickerView*)pivkerView numberOfRowsInComponent:(NSInteger)component{ if(component == 0){ // 返回省份數組的長度 return [aProvince count]; }else{ //返回省份對應城市數組的長度。 [tempArray count]; } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if(component == 0){ //返回對應row 的數組元素。 return [aProvince objectAtIndex:row]; }else{ return [tempArray objectAtIndex:row]; } } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if(component == 0){ //返回省份對應的城市數組, tempArray = [aCity objectAtIndex:row] [picker selectRow:0 inComponent:1 animated:YES]; [picker reloadComponent:1]; strProvience = [aProvince objectAtIndex:row]; strCity = [tempArray objectAtIndex:0]; }else{ //城市滾輪滾動時,strCity也隨着改變。 strCity = [tempArray objectAtIndex:0]; } lblTitle.text = [[NSString alloc] initWithFormat:@"%@,%@",strProvince,strCity,nil]; } - (void)viewDidUnload { [self setLblTitle:nil]; [self setPicker:nil]; [super viewDidUnload]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } - (IBAction)btnClick:(id)sender { lblTitle.text = [[NSString alloc] initWithFormat:@"%@,%@",strProvince,strCity,nil]; } -(void)dealloc{ [aProvince release]; [aCity release]; [lblTitle release]; [strCity release]; [strProvince release]; [picker release]; [super dealloc]; } @end
@protocol UIPickerViewDataSource<NSObject> @required // returns the number of 'columns' to display. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; // returns the # of rows in each component.. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component; @end
UIPickerViewDelegate協議中的五個方法,非必須實現,
@protocol UIPickerViewDelegate<NSObject> @optional // returns width of column and height of row for each component. - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component; - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component; // these methods return either a plain UIString, or a view (e.g UILabel) to display the row for the component. // for the view versions, we cache any hidden and thus unused views and pass them back for reuse. // If you return back a different object, the old one will be released. the view will be centered in the row rect - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view; - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component; @end

