UIPickerView簡單使用

 先說一下當個組件選取器,我們創建一個數組NSAray來保存選取器中的內容;選取器本身不會儲存任何數據,,它通過調用數據源和委託方法來顯示數據;但是對於大量數據的數據源,數組並不合適,我們可以做一個靜態列表如plist文件或者URL載入,和後面將講在文件中獲取數據,還以多個選取器的之間的關聯如何實現;先說下簡單的單個選取器:

先把效果圖貼出來


1.新建工程名爲PickerViewDemo , File->New->Project ->single View Application -> next


2.在視圖上添加選取器

  1. pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];  
  2. //    指定Delegate  
  3.     pickerView.delegate=self;  
  4. //    顯示選中框  
  5.     pickerView.showsSelectionIndicator=YES;  
  6.     [self.view addSubview:pickerView];   
以上可以在視圖顯示一個選取器,但是內容空白,pickerView.showsSelectionIndicator=YES;是這隻當前選取器所選中的內容:

選取器上顯示數據,必須依賴兩個協議,UIPickerViewDelegate和UIPickerViewDataSource,把他們添加到ViewController.h文件中

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>  
  4. {  
  5.   
  6.     UIPickerView *pickerView;  
  7.     NSArray *pickerData;  
  8.   
  9. }  
  10.   
  11. @end  
3.然後在.m文件的ViewDidLoad中初始化界面

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view, typically from a nib.  
  5.       
  6.     pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];  
  7. //    指定Delegate  
  8.     pickerView.delegate=self;  
  9. //    顯示選中框  
  10.     pickerView.showsSelectionIndicator=YES;  
  11.     [self.view addSubview:pickerView];   
  12.       
  13.     NSArray *dataArray = [[NSArray alloc]initWithObjects:@"許嵩",@"周杰倫",@"梁靜茹",@"許飛",@"鳳凰傳奇",@"阿杜",@"方大同",@"林俊杰",@"胡夏",@"邱永傳", nil];  
  14.       
  15.     pickerData=dataArray;  
  16.       
  17. //     添加按鈕     
  18.     CGRect frame = CGRectMake(120, 250, 80, 40);  
  19.     UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  20.     selectButton.frame=frame;  
  21.     [selectButton setTitle:@"SELECT" forState:UIControlStateNormal];  
  22.       
  23.     [selectButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];  
  24.       
  25.     [self.view addSubview:selectButton];  
  26.       
  27. }  

4.實現UIPickerView的代理方法,將數據顯示在選取器上所需要幾個方法

  1. #pragma mark -  
  2. #pragma mark Picker Date Source Methods  
  3.   
  4. //返回顯示的列數  
  5. -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView  
  6. {  
  7.     return 1;  
  8. }  
  9. //返回當前列顯示的行數  
  10. -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component  
  11. {  
  12.     return [pickerData count];  
  13. }  
  14.   
  15. #pragma mark Picker Delegate Methods  
  16.   
  17. //返回當前行的內容,此處是將數組中數值添加到滾動的那個顯示欄上  
  18. -(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component  
  19. {  
  20.     return [pickerData objectAtIndex:row];  
  21. }  
前兩個是數據源的代理方法,一個是返回列,有幾個選取器就返回幾,第二個是設置選取器有多少行,因爲就這一個選取器,所以直接返回行數,即數組元素個數多少;第三個代理方法是將數組元素添加到了選取器上面顯示


說一下兩個協議實例方法,參考http://www.cnblogs.com/edsioon/


UIPickerViewDelegate中的實例方法

-(void) pickerView: (UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent: (NSInteger)component

作用: 當用戶選擇某個row時,picker view調用此函數

參數: pickerView representing the picker view request the data

-(CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent: (NSInteger) component

作用:由picker view調用,當其在繪製row內容,需要row的高度時

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) component

作用: 當picker view需要給指定的component.row指定title時,調用此函數

-(UIView *)pickerView: (UIPickerView *)pickerView view ForRow:(NSInteger) row forComponent:(NSInteger) component reusingView:(UIView *) view

作用: 當picker view需要給指定的component.row指定view時,調用此函數.返回值爲用作row內容的view

參數: view參數, 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

作用:當picker view 需要row的寬度時,調用此函數


UIPickerViewDataSource中的實例方法

按照官方文檔的說法,UIPickerViewDataSource這個協議僅有的功能就是提供picker view中component的個數和各個component中的row的個數,雖然名爲datasource,但是它工作於MVC的C中

本協議僅有兩個實例方法,均需要實現

-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView

作用:返回pickerView應該有幾個component

-(NSInteger) pickerView:(UIPickerView *) pickerView numberOfRowsInComponent:(NSInteger) component

作用:返回指定component應該有幾個row



5.關於按鈕響應事件,關於按鈕的形成和添加響應事件不再提,前面都有,

  1. -(void) buttonPressed:(id)sender  
  2. {  
  3.      NSInteger row =[pickerView selectedRowInComponent:0];  
  4.      NSString *selected = [pickerData objectAtIndex:row];  
  5.      NSString *message = [[NSString alloc] initWithFormat:@"你選擇的是:%@",selected];  
  6.       
  7.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"   
  8.                                                     message:message  
  9.                                                    delegate:self  
  10.                                           cancelButtonTitle:@"OK"  
  11.                                           otherButtonTitles: nil];  
  12.     [alert show];  
  13.       
  14. }  

[pickerViewselectedRowInComponent:0];方法返回當前被選中的索引序號,這是UIPickerView的實例方法,在官方文檔中


UIPickerView還有其他實例方法

- (NSInteger) numberOfRowsInComponent:(NSInteger)component

參數爲component的序號(從左到右,以0起始),返回指定的component中row的個數

-(void) reloadAllComponents

調用此方法使得PickerView向delegate: Query for new data for all components

-(void) reloadComponent: (NSInteger) component

參數爲需更新的component的序號,調用此方法使得PickerView向其delegate: Query for new data

-(CGSize) rowSizeForComponent: (NSInteger) component

參數爲component的序號,返回值爲the size of rows in the given components, picker view 通過調用委託方法中的pickerView:widthForComponent:和pickerView:rowHeightForComponent:獲得返回值

-(NSInteger) selectedRowInComponent: (NSInteger) component

參數爲component的序號,返回被選中row的序號,若無row被選中,則返回-1

-(void) selectRow: (NSInteger)row inComponent: (NSInteger)component animated: (BOOL)animated

作用:在代碼指定要選擇的某component的某row

參數:row序號,component序號,BOOL值(若爲YES,轉動spin到你選擇的新值,若爲NO,直接顯示你選擇的值)

-(UIView *) viewForRow: (NSInteger)row forComponent: (NSInteger)component

參數:row序號,component序號,返回由委託方法pickerView:viewForRow:forComponentreusingView:指定的view.如果委託對象並沒有實現這個方法,或者說這個view並不是可見的,則返回nil



附上源代碼: http://download.csdn.net/detail/duxinfeng2010/4410909

發佈了4 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章