iOS-UIDatePicker、UIPickerView及键盘处理

一、如何监听控件的一些事件 或者 行为
* 如果父类是UIControl,说明是通过addTarget:action:forControlEvents:方法来监听控件的一些事件
* 如果父类不是UIControl,说明一般是通过代理方法来监听控件的一些行为

二、UIDatePicker
1.常见属性
/*
 样式
 UIDatePickerModeTime,时间
 UIDatePickerModeDate,日期
 UIDatePickerModeDateAndTime 日期 + 时间
 */
@property(nonatomic) UIDatePickerMode datePickerMode;

/*
 区域
 中国:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]
 */
@property(nonatomic,retain) NSLocale      *locale;

2.事件监听
1> 因为它继承自UIControl,所以跟按钮一样监听
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
2> 事件类型:UIControlEventValueChanged

三、UIPickerView
1.需要靠dataSource和delegate来显示数据
@property(nonatomic,assign) id<UIPickerViewDataSource> dataSource;  
@property(nonatomic,assign) id<UIPickerViewDelegate>   delegate;

2.常见数据源和代理方法
1> 数据源方法
// 一共有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

// 第component列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

2> 代理方法
// 第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;
// 选中了第component列第row行就会调用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

3.常见方法
1> 是否显示“选中指示器”
@property(nonatomic) BOOL showsSelectionIndicator;

2> 刷新数据(重新调用数据源和代理的方法来显示数据)
- (void)reloadAllComponents; // 刷新所有的列
- (void)reloadComponent:(NSInteger)component; // 只刷新第component列

3> 通过代码选中第component列第row行
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;

4> 获得第component列所选中的行号
- (NSInteger)selectedRowInComponent:(NSInteger)component;

四、UITextField
1.常见属性
1> 键盘
@property (readwrite, retain) UIView *inputView;

2> 键盘顶部的工具条
@property (readwrite, retain) UIView *inputAccessoryView;

3> 代理
@property(nonatomic,assign) id<UITextFieldDelegate> delegate;

2.常见方法
1> 叫出键盘
- (BOOL)becomeFirstResponder;

2> 退出键盘
- (BOOL)resignFirstResponder;

3.常见代理方法
1> 如果返回NO,代表文本框不能编辑、不能弹出键盘
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;

2> 如果返回NO,代表禁止改变文本框的文字(不能增加、删除文字)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;

3> 点击了键盘右下角的按钮就会调用(return key)
- (BOOL)textFieldShouldReturn:(UITextField *)textField; 

五、UIButton的状态
UIControlStateNormal  // 默认、普通
UIControlStateHighlighted // 高亮(当用户长按的时候达到这种状态)
UIControlStateDisabled // 不可用(这种状态下的按钮不能处理任何点击事件,enabled = NO的时候就能达到这种状态)
// 当切换状态的时候,按钮就会显示对应状态的背景图片、小图片、文字、文字颜色

六、控件的封装
1.为什么封装?
1> 重用某个常用的功能
2> 屏蔽某个功能的实现细节

2.封装的步骤
1> 自定义一个View(新建一个继承UIView的类)
2> 如果控件内部的内容是固定的,可以用一个xib文件来描述所封装控件内部的细节
3> 所封装控件内部的事件,应该通过代理传递出去
* 当所封装控件内部发生了一些事情,应该通知代理,代理得知内部的事件后,就可以在代理方法中实现想做的事情
一、UITextField的代理方法
#pragma mark 当文本框开始编辑的时候调用---开始聚焦
- (void)textFieldDidBeginEditing:(UITextField *)textField

二、排序
1.可变数组的排序(NSMutableArray)
* sortUsingComparator:方法调完,会直接改变array这个可变数组内部对象的顺序
[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    
}];

* 排序过程中会不断地调用block,传入两个需要比较的对象:id obj1, id obj2

* block必须有返回值:NSComparisonResult

* NSComparisonResult有3种取值:
NSOrderedAscending = -1L, // 右边的对象排后面
NSOrderedSame, // 一样
NSOrderedDescending // 左边的对象排后面

2.不可变数组的排序(NSArray)
* sortedArrayUsingComparator:方法并不会改变array数组内部的顺序
* sortedArrayUsingComparator:方法会返回一个新的已经排好序的数组sortedArray
NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    
}];

三、监听键盘的显示和隐藏
1.监听键盘通知
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

// 1.显示键盘
[center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

// 2.隐藏键盘
[center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

2.移除键盘通知(非ARC必须写)
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

3.注意点:当弹出一个新的键盘时,才会发出显示键盘的通知
一、UITextField的代理方法
#pragma mark 当文本框开始编辑的时候调用---开始聚焦
- (void)textFieldDidBeginEditing:(UITextField *)textField

二、排序
1.可变数组的排序(NSMutableArray)
* sortUsingComparator:方法调完,会直接改变array这个可变数组内部对象的顺序
[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    
}];

* 排序过程中会不断地调用block,传入两个需要比较的对象:id obj1, id obj2

* block必须有返回值:NSComparisonResult

* NSComparisonResult有3种取值:
NSOrderedAscending = -1L, // 右边的对象排后面
NSOrderedSame, // 一样
NSOrderedDescending // 左边的对象排后面

2.不可变数组的排序(NSArray)
* sortedArrayUsingComparator:方法并不会改变array数组内部的顺序
* sortedArrayUsingComparator:方法会返回一个新的已经排好序的数组sortedArray
NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    
}];

三、监听键盘的显示和隐藏
1.监听键盘通知
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

// 1.显示键盘
[center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

// 2.隐藏键盘
[center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

2.移除键盘通知(非ARC必须写)
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

3.注意点:当弹出一个新的键盘时,才会发出显示键盘的通知
原文地址:https://www.cnblogs.com/DarbyCJ/p/3658791.html