本文主要讲述UITextfield的delegate在iOS开发过程中的一些应用
一、到达到textfield在不同输入状态下的不同效果,先看一下实际效果
下面直接实现
//定义两个属性且要实现代理
@interface LoginViewController()<UITextFieldDelegate>
@property (strong, nonatomic) UITextField *photoText;
@property (strong, nonatomic) UITextField *passwordText;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(@"textFieldShouldBeginEditing");
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
NSLog(@"textFieldShouldEndEditing");
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"textFieldDidBeginEditing");
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"textFieldDidEndEditing");
}
当你点击textfield然后又离开则会显示四种状态,打印如下
//基本布局
//手机号textfield
self.photoText = [[UITextField alloc]initWithFrame:CGRectMake(100, self.login.y, self.view.width-140, 50)];
self.photoText.keyboardType = UIKeyboardTypeNumberPad;
self.photoText.tintColor = [UIColor lightGrayColor];
self.photoText.placeholder = @"请输入手机号";
self.photoText.background = [UIImage imageNamed:@"未填"];
self.photoText.textAlignment = NSTextAlignmentLeft;
self.photoText.clearButtonMode = UITextFieldViewModeWhileEditing;
self.photoText.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
self.photoText.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:self.photoText];
//密码textfield
self.passwordText = [[UITextField alloc]initWithFrame:CGRectMake(100, self.login.y+100, self.view.width-140, 50)];
self.passwordText.tintColor = [UIColor lightGrayColor];
self.passwordText.placeholder = @"请输入密码";
self.passwordText.background = [UIImage imageNamed:@"未填"];
self.passwordText.textAlignment = NSTextAlignmentLeft;
self.passwordText.clearButtonMode = UITextFieldViewModeWhileEditing;
self.passwordText.secureTextEntry = YES;
[self.view addSubview:self.passwordText];
self.photoText.delegate = self;
self.passwordText.delegate = self;
//视图上移的方法
- (void) animateTextField: (UITextField *) textField up: (BOOL) up
{
//设置视图上移的距离,单位像素,我此间上移20,根据需求自己可变
const int movementDistance = 20; // tweak as needed
//三目运算,判定是否需要上移视图或者不变
int movement = (up ? -movementDistance : movementDistance);
//设置动画的名字
[UIView beginAnimations: @"Animation" context: nil];
//设置动画的开始移动位置
[UIView setAnimationBeginsFromCurrentState: YES];
//设置动画的间隔时间
[UIView setAnimationDuration: 0.20];
//设置视图移动的位移
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
//设置动画结束
[UIView commitAnimations];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
[self animateTextField: textField up: YES];
if ([self.photoText isEditing]) {
self.photoText.background = [UIImage imageNamed:@"选择"];
}
if ([self.passwordText isEditing]) {
self.passwordText.background = [UIImage imageNamed:@"选择"];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
[self animateTextField: textField up: NO];
//判断手机号,此处有正则判断
if ([YLUtlis checkTelNumber:self.photoText.text]) {
self.photoText.background = [UIImage imageNamed:@"正确"];
}
if (![YLUtlis checkTelNumber:self.photoText.text]){
self.photoText.background = [UIImage imageNamed:@"错误"];
}
if ([self.photoText.text isEqualToString:@""]) {
self.photoText.background = [UIImage imageNamed:@"未填"];
}
//判断密码,此处有正则判断
if ([YLUtlis checkPassword:self.passwordText.text] ) {
self.passwordText.background = [UIImage imageNamed:@"正确"];
}
if(![YLUtlis checkPassword:self.passwordText.text] ){
self.passwordText.background = [UIImage imageNamed:@"错误"];
}
if ([self.passwordText.text isEqualToString:@""]) {
self.passwordText.background = [UIImage imageNamed:@"未填"];
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
return YES;
}
二、为了能够监听UITextField的值是“成功”变化,来决定保存按钮是否出现(和微信那种成功输入然后button由不可点击到可点击原理类似)
刚进入界面,保存button处于隐藏状态
成功输入了值或是减少了值(不是点击textfield处于选中状态),出现button
@interface YLUpdataUserNameViewController ()<UITextFieldDelegate>
{
UITextField *_nameTxtfild;
UIButton *_saveBtn;
UILabel *_promptLabel;
}
@end
@implementation YLUpdataUserNameViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initView];
self.tabBarController.hidesBottomBarWhenPushed = YES;
self.title = @"用户昵称";
_nameTxtfild.delegate = self;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//string是textfield里面改变的东西,所以只需判断string即可
if (string!=nil) {
self.navigationItem.rightBarButtonItem.customView.hidden = NO;
}
return YES;
}
- (void)initView
{
//label
self.view.backgroundColor = [UIColor whiteColor];
_nameTxtfild = [[UITextField alloc]initWithFrame:CGRectMake(20, 80, WindowWidth-40, 30)];
[_nameTxtfild setTextAlignment:NSTextAlignmentLeft];
[_nameTxtfild setBackground:[UIImage imageNamed:@"userName_textfield"]];//图片是一条线,可更具自己需求
_nameTxtfild.text = self.nameString;//(涉及属性传值,此处不赘言)
[self.view addSubview:_nameTxtfild];
//提示信息
_promptLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 120, WindowWidth-40, 15)];
_promptLabel.text = @"给自己一个好的名字吧!!!";
_promptLabel.textColor = [UIColor lightGrayColor];
_promptLabel.font = [UIFont systemFontOfSize:12.0];
[self.view addSubview:_promptLabel];
// 右上加号按钮
_saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_saveBtn setTitle:@"保存" forState:UIControlStateNormal];
[_saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_saveBtn.titleLabel.font = [UIFont systemFontOfSize:15.f];
_saveBtn.enabled = NO;
// 添加监听方法,此处方法省略saveName
[_saveBtn addTarget:self action:@selector(saveName) forControlEvents:UIControlEventTouchUpInside];
_saveBtn.size = CGSizeMake(35, 30);
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:_saveBtn];
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor blueColor]];
self.navigationItem.rightBarButtonItem.customView.hidden = YES;
}