iOS:选择器控件UIPickerView的详解和演示

选择器控件UIPickerView:

功能:它能够创建一个类似于密码锁式的单列或多列的选择菜单,用户可以通过它设置的代理来选择需要菜单中的任意的数据。例如创建日历、字体表(类型、大小、颜色)、图库等.
 
它是UIView视图的子类,而且遵从UIPickerViewDelegate协议和UIPickerViewDataSource协议。因此,选择器需要设置代理去进行操作;
设置视图控制器self为数据源代理和数据操作代理,即
@property(strong,nonatomic)UIPickerView *pickerView;
self.pickerView.dataSource = self;
self.pickerView.delegate  = self;
 
一、具体类信息:

 @protocol UIPickerViewDataSource, UIPickerViewDelegate;

 @interface UIPickerView : UIView <NSCoding>

 @property(nonatomic,assign) id<UIPickerViewDataSource> dataSource; // default is nil,设置代理

 @property(nonatomic,assign) id<UIPickerViewDelegate>   delegate;  // default is nil,设置代理

 @property(nonatomic)  BOOL showsSelectionIndicator;    // default is NO

 @property(nonatomic,readonly) NSInteger numberOfComponents; //列数

 

二、协议UIPickerViewDataSource

@protocol UIPickerViewDataSource<NSObject>

@required //必须要实现的方法

// 返回的列显示的数量。

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

//返回行数在每个组件(每一列)

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

@end

 

三、协议UIPickerViewDelegate

@protocol UIPickerViewDelegate<NSObject>

@optional //可以选择执行的方法

//每一列组件的列宽度

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;

//每一列组件的行高度

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;

// 返回每一列组件的每一行的标题内容

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

// 返回每一列组件的每一行的标题内容的属性

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component;

// 返回每一列组件的每一行的视图显示

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;

//执行选择某列某行的操作

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

@end

 
   
演示一:制作简单的万年历,一开始就显示当前日期
源码如下:
复制代码
1 #import <UIKit/UIKit.h>
2 
3 @interface ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
4 @property(nonatomic,strong)NSArray *years;
5 @property(nonatomic,strong)NSArray *months;
6 @property(nonatomic,strong)NSArray *days;
7 
8 @end
复制代码
  View Code

 

演示二:制作简单的字体表,包括字体类型、大小、颜色

源码如下:

复制代码
1 #import <UIKit/UIKit.h>
2 
3 @interface ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
4 @property(nonatomic,strong) NSArray *fontNames;
5 @property(nonatomic,strong) NSArray *fontSizes;
6 @property(nonatomic,strong) NSArray *fontColors;
7 @end
复制代码
  View Code

  

演示三:制作简单的图库浏览器

源码如下:

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>
@end
  View Code

 

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!


本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4767710.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值