IOS開發之旅(三)----UIPickerView


一、UIPickerView常用方法

  1、UIPickerView和UIDatePicker是類似的控件,只不過UIDatePicker是日期控件,只能放日期,而UIPickerView可以放任何東西。

  2、UIPickerView例子

  

  3、UIPickerView代理

    @property(nonatomic,assign)

      id<UIPickerViewDelegate> delegate;

    @property(nonatomic,assign)

      id<UIPickerViewDataSource>dataSource;

    delegate 定義了UIPickerView的外觀和屬性

    dataSource 定義了UIPickerView的數據源和定制內容

  4、UIPickerView常用方法

    1>返回component列,row行的一個UIView,這里只有在定制的情況下才有小,其他情況返回nil

    2>- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;

    3>重新裝在整個UIPickerView所有列的數據和指定列的數據

    4>- (void) reloadAlllComponents;

    5>- (void) reloadComponent:(NSInteger)component;

    6>現在UIPickerView中component列,row行,也就是讓改行滾動到中央

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

    8>返回定制列component中選中的行,沒有選中返回-1;

    9>- (NSInteger)selectedRowInComponent:(NSInteger)component;

二、UIPickerViewDataSource

  1、返回UIPickerView一共有幾列

    - (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView;

  2、返回定制的component列有幾行數據

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

三、UIPickerViewDelegate

  1、

    

  2、

     

  3、主要的使用的借個方法的代碼如下

    

View Code
#pragma mark-
#pragma mark PickerView function

/*表示UIPickerView一共有幾列*/
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
/*返回component這列有多少行數據*/
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [fonts count];
}
/*返回component這列 row這行里面的字符串是什么*/
- (NSString *) pickerView:(UIPickerView *)pickerView 
              titleForRow:(NSInteger)row 
             forComponent:(NSInteger)component
{
    return [fonts objectAtIndex:row];
}
/*當我們選擇UIPickerView中列為component,行為row的回調函數*/
- (void) pickerView:(UIPickerView *)pickerView 
       didSelectRow:(NSInteger)row 
        inComponent:(NSInteger)component
{
    /*設置字體的樣式*/
    NSLog(@"font %d is selected",row);
    NSString *fontName=[fonts objectAtIndex:row];
    fontLabel.font=[UIFont fontWithName:fontName size:20.0f];
    fontLabel.text=[NSString stringWithFormat:@"字體 %@ 選中了",fontName];
}

 

四、定制UIPickerView

  1、

       

  

    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM