iOS学习-UITextField代理

  1 //
  2 //  ViewController.m
  3 //  UITextField代理
  4 //
  5 //  Created by 大欢 on 16/1/22.
  6 //  Copyright © 2016年 bjsxt. All rights reserved.
  7 //
  8
  9 #import "ViewController.h"
 10
 11 @interface ViewController ()<UITextFieldDelegate>
 12
 13 @property (nonatomic, strong) UITextField * userTF;
 14
 15 @property (nonatomic, strong) UITextField * pwdTF;
 16
 17 @end
 18
 19 @implementation ViewController
 20
 21 - (void)viewDidLoad {
 22     [super viewDidLoad];
 23
 24     UITextField * userTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, CGRectGetWidth(self.view.frame) - 40, 50)];
 25     userTextField.placeholder = @"请输入用户名";
 26     userTextField.borderStyle = UITextBorderStyleRoundedRect;
 27     userTextField.delegate = self;
 28     userTextField.clearButtonMode = UITextFieldViewModeAlways;
 29     userTextField.returnKeyType = UIReturnKeyNext;
 30     [self.view addSubview:userTextField];
 31     self.userTF = userTextField;
 32
 33     UITextField * pwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(userTextField.frame) + 20, CGRectGetWidth(self.view.frame) - 40, 50)];
 34     pwdTextField.placeholder = @"请输入密码";
 35     pwdTextField.borderStyle = UITextBorderStyleRoundedRect;
 36     pwdTextField.delegate = self;
 37     pwdTextField.returnKeyType = UIReturnKeyGo;
 38     [self.view addSubview:pwdTextField];
 39     self.pwdTF = pwdTextField;
 40
 41     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidBeginEditingNotification:) name:UITextFieldTextDidBeginEditingNotification object:nil];
 42     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidEndEditingNotification:) name:UITextFieldTextDidEndEditingNotification object:nil];
 43     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:nil];
 44
 45 }
 46
 47 - (void)textDidBeginEditingNotification:(NSNotification *)notifi {
 48     NSLog(@"开始");
 49 }
 50
 51 - (void)textDidEndEditingNotification:(NSNotification *)notifi {
 52     NSLog(@"结束");
 53 }
 54
 55 - (void)textDidChangeNotification:(NSNotification *)notifi {
 56
 57     UITextField * tf = notifi.object;
 58     NSLog(@"%@",tf.text);
 59 }
 60
 61 //是否可以开始编辑
 62
 63 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
 64
 65     return YES;
 66 }
 67
 68 //是否可以结束编辑
 69
 70 //- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
 71 //
 72 //    return YES;
 73 //}
 74 //
 75 //- (void)textFieldDidBeginEditing:(UITextField *)textField {
 76 //
 77 //    NSLog(@"已经开始");
 78 //}
 79 //
 80 //- (void)textFieldDidEndEditing:(UITextField *)textField {
 81 //
 82 //    NSLog(@"已经结束");
 83 //}
 84 //
 85 //- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
 86 //
 87 //    if (range.location > 2) return NO;
 88 //
 89 //    NSLog(@"%@",NSStringFromRange(range));
 90 //
 91 //    NSLog(@"%@",string);
 92 //
 93 //
 94 //    return YES;
 95 //}
 96
 97 //clearbutton 如果是NO不好使
 98
 99 //- (BOOL)textFieldShouldClear:(UITextField *)textField {
100 //
101 //    return NO;
102 //}
103
104
105 - (BOOL)textFieldShouldReturn:(UITextField *)textField {
106
107     if (self.userTF == textField) {
108         [self.pwdTF becomeFirstResponder];
109     } else if (self.pwdTF == textField) {
110         [self.pwdTF resignFirstResponder];
111     }
112
113     return YES;
114 }
115
116
117
118 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
119
120     [self.userTF resignFirstResponder];
121
122     [self.pwdTF resignFirstResponder];
123
124 }
125
126
127 @end
时间: 2024-08-30 00:08:12

iOS学习-UITextField代理的相关文章

IOS学习3——代理

本文转载自:你真的了解iOS代理设计模式吗? 在项目中我们经常会用到代理的设计模式,这是iOS中一种消息传递的方式,也可以通过这种方式来传递一些参数.这篇文章会涵盖代理的使用技巧和原理,以及代理的内存管理等方面的知识.我会通过这些方面的知识,带大家真正领略代理的奥妙.写的有点多,但都是干货,我能写下去,不知道你有没有耐心看下去.本人能力有限,如果文章中有什么问题或没有讲到的点,请帮忙指出,十分感谢! 一.iOS中消息传递方式 在iOS中有很多种消息传递方式,这里先简单介绍一下各种消息传递方式.

iOS之UI学习-UITextField代理篇

</pre><pre name="code" class="objc">#import "ViewController.h" //签订代理协议 @interface ViewController ()<UITextFieldDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //须知:U

IOS开发-UITextField代理常用的方法总结

1.//当用户全部清空的时候的时候 会调用 -(BOOL)textFieldShouldClear:(UITextField *)textField: 2.//可以得到用户输入的字符 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string: 3.//已经开始编辑的时候 会触发这个方法— - (void)te

iOS学习-UITextField

1 // 2 // ViewController.m 3 // UITextField详解 4 // 5 // Created by 大欢 on 16/1/21. 6 // Copyright © 2016年 bjsxt. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 @property (nonatomic, strong) UITextFi

iOS学习-UITextField设置placeholder的颜色

UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(50, 200, 200, 40)]; text.borderStyle = UITextBorderStyleRoundedRect; NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc]initWithString:@"密码"]; [attribut

2015最新iOS学习线路图

iOS是由苹果公司开发的移动操作系统,以xcode为主要开发工具,具有简单易用的界面.令人惊叹的功能,以及超强的稳定性,已经成为iPhone.iPad 和iPod touch 的强大基础:iOS 内置的众多技术和功能让 Apple设备始终保持着遥遥领先的地位. iOS学习路线:http://www.mobiletrain.org/page/ios.html 课程分  类 课程模块 模块介绍 课程内容 Part1C语言 C语言和Objective-C语言 C语言 Mac系统及常用工具.进制:C数据

我的IOS学习资源收录

IOS7视频教程-storyboard与UIApplication http://v.youku.com/v_show/id_XNzMxMjgwNzEy.html 关东升老师的ios视频教程 iPhone与iPad开发实战-ios经典应用剖析-7大项目实战开发 1第1讲工具类应用密码生成(Amuck Password Generator)57分钟   2第2讲工具类应用密码生成(Amuck Password Generator)52分钟   3第3讲工具类应用--密码生成(Amuck Passw

IOS的UITextField,UIButton,UIWebView的一些属性介绍和IOS图片资源的使用技巧

有时候UI给开发的资源跟实际的frame不一致,这个时候我们就要去拉伸图片 UIImage* image = [[UIImage imageNamed:@"text_field_bg.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:0]; //stretchableImageWithLeftCapWidth使图片有拉伸效果 UITextField的属性介绍: UITextField* field = [[UITextFiel

iOS学习笔记(2)— UIView用户事件响应

iOS学习笔记(2)— UIView用户事件响应 UIView除了负责展示内容给用户外还负责响应用户事件.本章主要介绍UIView用户交互相关的属性和方法. 1.交互相关的属性 userInteractionEnabled 默认是YES ,如果设置为NO则不响应用户事件,并且把当前控件从事件队列中删除.也就是说设置了userInterfaceEnabled属性的视图会打断响应者链导致该view的subview都无法响应事件. multipleTouchEnabled  默认是NO,如果设置为YE