UIPickView与UIDatePicker的使用

介绍UIPickView和UIDatePicker

1.UIPickView什么时候用?
  • 通常在注册模块,当用户需要选择一些东西的时候,比如说城市,往往弹出一个PickerView给他们选择。
  • 老虎机效果
2.UIPickView常见用法
  • 独立的,没有任何关系 => 菜单系统。
  • 相关联的,下一列和第一列有联系=> 省会城市选择
  • 图文并帽, => 国旗选择。
3.UIPickView

区别

4.UIDatePicker什么时候用?
  • 当用户选择日期的时候,一般弹出一个UIDatePicker给用户选择。
5.UIDatePicker iOS6和iOS7的区别

区别

UIPickView与UIDatePicker使用

UIPickView效果图

显示的界面效果

plist文件

plist

实现过程

  • 1.1:设置_pickerView的数据源及代理为控制器
  • 1.2:控制器遵守数据源,代理协议
  • 1.3:实现数据源方法
// 返回pickerView有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return self.foods.count;
}

// 返回第component列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSArray *arr = self.foods[component];
    return arr.count;
}
  • 1.4:实现代理方法
// 返回第component列第row行标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSArray *arr = self.foods[component];

    return arr[row];
}
// 监听用户的选中
// 选中第component列第row行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"%ld %ld",component, row);
    // 获取用户选中的标题
    NSString *title = self.foods[component][row];
    _labelView.text = title;

}
  • 1.5:其它代理方法
// 返回第component列有多宽
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;

// 返回第component列有高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;

// 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:
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值