iOS-UIPickerView拾取器

UIPickerView

UIPickerView是一個拾取器,UIPickerView和UIDatePicker是類似的控件,只不過UIDatePicker是日期控件,只能放日期,而UIPickerView可以放任何東西。它比UIDatePicker更加通用,它可以生成單列的選擇器,也可生成多列的選擇器,而且開發者完全可以自定義選擇項的外觀,因此用法非常靈活。UIPickerView直接繼承了UIView,沒有繼承UIControl,因此,它不能像UIControl那樣綁定事件處理方法,UIPickerView的事件處理由其委託對象完成。使用UIPickerView的對象應該遵守UIPickerViewDataSource,UIPickerViewDelegate。

 

常用屬性和方法:

 

1、numberOfComponents:獲取UIPickerView指定列中包含的列表項的數量。該屬性是一個只讀屬性。 

2showsSelectionIndicator:該屬性控制是否顯示UIPickerView中的選中標記(以高亮背景作爲選中標記)。 

3-numberOfRowsInComponent::獲取UIPickerView包含的列數量。 

4- rowSizeForComponent::獲取UIPickerView包含的指定列中列表項的大小。該方法返回一個CGSize對象。 

5- selectRow:inComponent:animated::該方法設置選中該UIPickerView中指定列的特定列表項。最後一個參數控制是否使用動畫。

6- selectedRowInComponent::該方法返回該UIPickerView指定列中被選中的列表項。 

7- viewForRow:forComponent::該方法返回該UIPickerView指定列的列表項所使用的UIView控件。

 

注意:UIDatePicker控件只是負責該控件的通用行爲,而該控件包含多少列,各列包含多少個列表項則由UIPickerViewDataSource對象負責。開發者必須爲UIPickerView設置UIPickerViewDataSource對象,並實現如下兩個方法。 

   

-numberOfComponentsInPickerView::該UIPickerView將通過該方法來判斷應該包含多少列。   


-pickerView:numberOfRowsInComponent::該UIPickerView將通過該方法判斷指定列應該包含多少個列表項 


如果程序需要控制UIPickerView中各列的寬度,以及各列中列表項的大小和外觀,或程序需要爲UIPickerView的選中事件提供響應,都需要爲UIPickerView設置UIPickerViewDelegate委託對象,並根據需要實現該委託對象中的如下方法。   

 

- pickerView:rowHeightForComponent::該方法返回的CGFloat值將作爲該UIPickerView控件中指定列中列表項的高度。   


-pickerView:widthForComponent::該方法返回的CGFloat值將作爲該UIPickerView控件中指定列的寬度。   

 

- pickerView:titleForRow:forComponent::該方法返回的NSString值將作爲該UIPickerView控件中指定列的列表項的文本標題。   


-pickerView:viewForRow:forComponent:reusingView::該方法返回的UIView控件將直接作爲該UIPickerView控件中指定列的指定列表項。   

-pickerView:didSelectRow:inComponent::當用戶單擊選中該UIPickerView控件的指定列的指定列表項時將會激發該方法。 

 

PickerVIew的簡單使用(先以單列選擇器爲例)


#import "ViewController.h"

#define SCREEN_BOUNDS [UIScreen mainScreen].bounds//屏幕尺寸

#define SCREEN_WIDTH CGRectGetWidth([UIScreenmainScreen].bounds)//屏幕寬

#define SCREEN_HEIGHT CGRectGetHeight([UIScreenmainScreen].bounds)//屏幕高

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>//掛代理

{  UIPickerView *pickerView;//創建pickerVIew

  

   NSArray *dataArray;//創建數據源數組

} @end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    dataArray = @[@"1",@"2",@"3",@"4",@"5",@"6"];

    pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT/2, SCREEN_WIDTH, SCREEN_HEIGHT/2)];

    [self.view addSubview:pickerView];

    pickerView.delegate = self;

    pickerView.dataSource = self;

}

#pragma mark 代理

//UIPickerViewDataSource中定義的方法,該方法的返回值決定該控件包含的列數

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

    return 1;

    // 返回1表明該控件只包含1

}

    //UIPickerViewDataSource中定義的方法,該方法的返回值決定該控件指定列包含多少個列表項

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

    // 由於該控件只包含一列,因此無須理會列序號參數component

// 該方法返回teams.count,表明teams包含多少個元素,該控件就包含多少行

    return dataArray.count;

}

/* UIPickerViewDelegate中定義的方法  該方法返回的NSString將作爲UIPickerView中指定列和列表項的標題文本  */

- (NSString *)pickerView:(UIPickerView*)pickerView  titleForRow:(NSInteger)rowforComponent:(NSInteger)component{

    // 由於該控件只包含一列,因此無須理會列序號參數component

    // 該方法根據row參數返回teams中的元素,row參數代表列表項的編號,

    // 因此該方法表示第幾個列表項,就使用teams中的第幾個元素

    return [dataArrayobjectAtIndex:row];

}

// 當用戶選中UIPickerViewDataSource中指定列和列表項時激發該方法

- (void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)rowinComponent:(NSInteger)component{

// 使用一個UIAlertView來顯示用戶選中的列表項

   NSString *alertString = [NSString stringWithFormat:@"選中了第%ld,%ld",component,row];

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"選中信息" message:alertString preferredStyle:UIAlertControllerStyleAlert];

    

    UIAlertAction *action = [UIAlertAction actionWithTitle:@"好的"style:UIAlertActionStyleDestructive handler: ^(UIAlertAction * _Nonnullaction){

    

       }];

    [alert addAction:action];

    [self presentViewController:alert animated:YES completion:nil];

}

 

 

@end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章