您的位置:首页 > 产品设计 > UI/UE

四. 控件-UIPickerView

2016-06-05 15:02 369 查看

应用场景

一般用在用户注册,或者有时间设置的设置页面;

iOS6 与 iOS7 控件的显示是不同的样式;



简单使用

设置代理(初始化后)

self.pickerView.delegate = self;
self.pickerView.dateSource = self;
>注意:<UIPickerViewDelgate,UIPickerViewDateSource>


必须实现的代理方法

//返回有多少列
-(NSInterger)numberOfComponentsInPickerView:(UIPickerView*)pickerView;
//返回每列有多少行
-pickerView:(NSInterger(UIPickerView*)pickerView numberOfR  owinComponent:(NSInteger)compoment;


3.可选代理方法

#pragma mark -UIPickerViewDelegate
// 返回第component列高度
//- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
//{
//
//    return 100;
//}

// 返回第component列第row行的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 1 && row == 1) {
return @"bcd";
}
return @"abc";
}
// NSAttributedString:给文本添加一些属性,富文本,设置文本的颜色,字体,空心,阴影,图文混排.
//- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0); // attributed title is favored if both methods are implemented

// 返回第component列第row行的控件
//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;

// 监听UIPickerView选中
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSLog(@"%ld %ld",component,row);
}


UIDatePickerView

// 创建日期选择控件
UIDatePicker *dateP = [[UIDatePicker alloc] init];

// 设置日期模式,年月日
dateP.datePickerMode = UIDatePickerModeDate;

// 设置地区 zh:中国标识
dateP.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];

[dateP addTarget:self action:@selector(dateChamge:) forControlEvents:UIControlEventValueChanged];

// 自定义文本框的键盘
self.inputView = dateP;
// 只要UIDatePicker选中的时候调用
- (void)dateChamge:(UIDatePicker *)picker
{
// 2015-09-06 yyyy-MM-dd
// 创建一个日期格式字符串对象
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
self.text = [fmt stringFromDate:picker.date];
}


UIPickerView的重要的代理

// 选中某一行的时候调用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == 0) { // 滚动省会
// 记录下选中的省会角标
_selProvinceIndex = row;

// 刷新第1列的城市
[pickerView reloadComponent:1];

// 让pickerView选中第1列第0行
[pickerView selectRow:0 inComponent:1 animated:YES];
}

// 获取选中的省
Province *p = self.provinces[_selProvinceIndex];

// 获取选中的城市
NSArray *cities = p.cities;

// 获取第1列选中的行
NSInteger cityIndex = [pickerView selectedRowInComponent:1];

// 获取选中的城市
NSString *cityName = cities[cityIndex];

// 给文本框赋值
self.text = [NSString stringWithFormat:@"%@ %@",p.name, cityName];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PickerView