基本
1.UIPickerView 屬性
數據源(用來告知UIPickerView有若干列若干行)
@property(nonatomic,assign) id dataSource;
署理(用來告知UIPickerView每1列的每1行顯示甚麼內容,監聽UIPickerView的選擇)
@property(nonatomic,assign) id delegate;
能否要顯示選中的指導器
@property(nonatomic) BOOL showsSelectionIndicator;
一共有若干列
@property(nonatomic,readonly) NSInteger numberOfComponents;
2.UIPickerView辦法
從新刷新一切列
- (void)reloadAllComponents;
從新刷新第component列
- (void)reloadComponent:(NSInteger)component;
自動選中第component列的第row行
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;
取得第component列確當前選中的行號
- (NSInteger)selectedRoWinComponent:(NSInteger)component;
3.UIPickerView數據源辦法
一共有若干列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
第component列一共有若干行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
4.UIPickerView署理辦法
第component列的寬度是若干
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
第component列的行高是若干
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;
第component列第row行顯示甚麼文字
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
第component列第row行顯示如何的view(內容)
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
選中了pickerView的第component列第row行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
實例
UIPickerView 作為IOS的一個經常使用控件信任年夜家都有這方面的需求。
明天我們就簡略創立一個:
新建項目 定名:TestUIPickerView
在默許生成的ViewController中創立UIPickerView
起首在viewDidLoad 的辦法中創立
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 選擇框
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, 320, 216)];
// 顯示選中框
pickerView.showsSelectionIndicator=YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[self.view addSubview:pickerView];
_proTimeList = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",nil];
_proTitleList = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",nil];
}
然後,我們創立相干的署理辦法
UIPickerViewDataSource 相干署理
#pragma Mark -- UIPickerViewDataSource
// pickerView 列數
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
// pickerView 每列個數
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {
return [_proTitleList count];
}
return [_proTimeList count];
}
UIPickerViewDelegate 相干署理辦法
#pragma Mark -- UIPickerViewDelegate
// 每列寬度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
if (component == 1) {
return 40;
}
return 180;
}
// 前往選中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == 0) {
NSString *_proNameStr = [_proTitleList objectAtIndex:row];
NSLog(@"nameStr=%@",_proNameStr);
} else {
NSString *_proTimeStr = [_proTimeList objectAtIndex:row];
NSLog(@"_proTimeStr=%@",_proTimeStr);
}
}
//前往以後行的內容,此處是將數組中數值添加到轉動的誰人顯示欄上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
return [_proTitleList objectAtIndex:row];
} else {
return [_proTimeList objectAtIndex:row];
}
}
完成以上代碼以後 我們便可以運轉項目檢查後果
以下圖:
【實例講授iOS運用開辟中UIPickerView轉動選擇欄的用法】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!