您好,登錄后才能下訂單哦!
先說一下當(dāng)個(gè)組件選取器,我們創(chuàng)建一個(gè)數(shù)組NSAray來保存選取器中的內(nèi)容;選取器本身不會(huì)儲(chǔ)存任何數(shù)據(jù),,它通過調(diào)用數(shù)據(jù)源和委托方法來顯示數(shù)據(jù);但是對(duì)于大量數(shù)據(jù)的數(shù)據(jù)源,數(shù)組并不合適,我們可以做一個(gè)靜態(tài)列表如plist文件或者URL載入,和后面將講在文件中獲取數(shù)據(jù),還以多個(gè)選取器的之間的關(guān)聯(lián)如何實(shí)現(xiàn);先說下簡單的單個(gè)選取器:
先把效果圖貼出來
1.新建工程名為PickerViewDemo , File->New->Project ->single View Application -> next
2.在視圖上添加選取器
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)]; // 指定Delegate pickerView.delegate=self; // 顯示選中框 pickerView.showsSelectionIndicator=YES; [self.view addSubview:pickerView];以上可以在視圖顯示一個(gè)選取器,但是內(nèi)容空白,pickerView.showsSelectionIndicator=YES;是這只當(dāng)前選取器所選中的內(nèi)容:
選取器上顯示數(shù)據(jù),必須依賴兩個(gè)協(xié)議,UIPickerViewDelegate和UIPickerViewDataSource,把他們添加到ViewController.h文件中
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource> { UIPickerView *pickerView; NSArray *pickerData; } @end3.然后在.m文件的ViewDidLoad中初始化界面
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)]; // 指定Delegate pickerView.delegate=self; // 顯示選中框 pickerView.showsSelectionIndicator=YES; [self.view addSubview:pickerView]; NSArray *dataArray = [[NSArray alloc]initWithObjects:@"許嵩",@"周杰倫",@"梁靜茹",@"許飛",@"鳳凰傳奇",@"阿杜",@"方大同",@"林俊杰",@"胡夏",@"邱永傳", nil]; pickerData=dataArray; // 添加按鈕 CGRect frame = CGRectMake(120, 250, 80, 40); UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; selectButton.frame=frame; [selectButton setTitle:@"SELECT" forState:UIControlStateNormal]; [selectButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:selectButton]; }
#pragma mark - #pragma mark Picker Date Source Methods //返回顯示的列數(shù) -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } //返回當(dāng)前列顯示的行數(shù) -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [pickerData count]; } #pragma mark Picker Delegate Methods //返回當(dāng)前行的內(nèi)容,此處是將數(shù)組中數(shù)值添加到滾動(dòng)的那個(gè)顯示欄上 -(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [pickerData objectAtIndex:row]; }前兩個(gè)是數(shù)據(jù)源的代理方法,一個(gè)是返回列,有幾個(gè)選取器就返回幾,第二個(gè)是設(shè)置選取器有多少行,因?yàn)榫瓦@一個(gè)選取器,所以直接返回行數(shù),即數(shù)組元素個(gè)數(shù)多少;第三個(gè)代理方法是將數(shù)組元素添加到了選取器上面顯示;
說一下兩個(gè)協(xié)議實(shí)例方法,參考http://www.cnblogs.com/edsioon/
UIPickerViewDelegate中的實(shí)例方法
-(void) pickerView: (UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent: (NSInteger)component
作用: 當(dāng)用戶選擇某個(gè)row時(shí),picker view調(diào)用此函數(shù)
參數(shù): pickerView representing the picker view request the data
-(CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent: (NSInteger) component
作用:由picker view調(diào)用,當(dāng)其在繪制row內(nèi)容,需要row的高度時(shí)
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) component
作用: 當(dāng)picker view需要給指定的component.row指定title時(shí),調(diào)用此函數(shù)
-(UIView *)pickerView: (UIPickerView *)pickerView view ForRow:(NSInteger) row forComponent:(NSInteger) component reusingView:(UIView *) view
作用: 當(dāng)picker view需要給指定的component.row指定view時(shí),調(diào)用此函數(shù).返回值為用作row內(nèi)容的view
參數(shù): view參數(shù), a view object that was previously used for this rows, but is now hidden and cached by the picker view
- (CGFloat)pickerView: (UIPickerView *)pickerView widthForComponent:(NSInteger) component
作用:當(dāng)picker view 需要row的寬度時(shí),調(diào)用此函數(shù)
UIPickerViewDataSource中的實(shí)例方法
按照官方文檔的說法,UIPickerViewDataSource這個(gè)協(xié)議僅有的功能就是提供picker view中component的個(gè)數(shù)和各個(gè)component中的row的個(gè)數(shù),雖然名為datasource,但是它工作于MVC的C中
本協(xié)議僅有兩個(gè)實(shí)例方法,均需要實(shí)現(xiàn)
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
作用:返回pickerView應(yīng)該有幾個(gè)component
-(NSInteger) pickerView:(UIPickerView *) pickerView numberOfRowsInComponent:(NSInteger) component
作用:返回指定component應(yīng)該有幾個(gè)row
5.關(guān)于按鈕響應(yīng)事件,關(guān)于按鈕的形成和添加響應(yīng)事件不再提,前面都有,
-(void) buttonPressed:(id)sender { NSInteger row =[pickerView selectedRowInComponent:0]; NSString *selected = [pickerData objectAtIndex:row]; NSString *message = [[NSString alloc] initWithFormat:@"你選擇的是:%@",selected]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; }
[pickerViewselectedRowInComponent:0];方法返回當(dāng)前被選中的索引序號(hào),這是UIPickerView的實(shí)例方法,在官方文檔中
UIPickerView還有其他實(shí)例方法
- (NSInteger) numberOfRowsInComponent:(NSInteger)component
參數(shù)為component的序號(hào)(從左到右,以0起始),返回指定的component中row的個(gè)數(shù)
-(void) reloadAllComponents
調(diào)用此方法使得PickerView向delegate: Query for new data for all components
-(void) reloadComponent: (NSInteger) component
參數(shù)為需更新的component的序號(hào),調(diào)用此方法使得PickerView向其delegate: Query for new data
-(CGSize) rowSizeForComponent: (NSInteger) component
參數(shù)為component的序號(hào),返回值為the size of rows in the given components, picker view 通過調(diào)用委托方法中的pickerView:widthForComponent:和pickerView:rowHeightForComponent:獲得返回值
-(NSInteger) selectedRowInComponent: (NSInteger) component
參數(shù)為component的序號(hào),返回被選中row的序號(hào),若無row被選中,則返回-1
-(void) selectRow: (NSInteger)row inComponent: (NSInteger)component animated: (BOOL)animated
作用:在代碼指定要選擇的某component的某row
參數(shù):row序號(hào),component序號(hào),BOOL值(若為YES,轉(zhuǎn)動(dòng)spin到你選擇的新值,若為NO,直接顯示你選擇的值)
-(UIView *) viewForRow: (NSInteger)row forComponent: (NSInteger)component
參數(shù):row序號(hào),component序號(hào),返回由委托方法pickerView:viewForRow:forComponentreusingView:指定的view.如果委托對(duì)象并沒有實(shí)現(xiàn)這個(gè)方法,或者說這個(gè)view并不是可見的,則返回nil
附上源代碼: http://download.csdn.net/detail/duxinfeng2010/4410909
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。