初始化
UIPickerView *flagPicker = [[UIPickerView alloc] init];
// 设置代理和数据源
flagPicker.dataSource = self;
flagPicker.delegate = self;
代理方法
// 每列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
}
// 返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
// 显示每行每列的数据
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
}
数据源方法
// 几列数据
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
}
// 每列的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
}
需求:点击按钮弹出键盘UIPickerView
将textFiled的键盘设置为pickerView;
然后将textFiled变为第一响应者,这是弹出的键盘就是自定义的UIPickerView键盘;
- (IBAction)onclick:(id)sender {
self.textFiled.inputView = self.pickerView;
[self.textFiled becomeFirstResponder];
}