IOS之监听UITextField的编辑事件

原创 2015年07月10日 10:24:50

1、如果需要在UIViewController中监听UITextField的编辑事件,可以在视图控制器以下方法进行处理

@protocol UITextFieldDelegate <NSObject>

@optional

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;        // return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField;           // became first responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;          // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (void)textFieldDidEndEditing:(UITextField *)textField;             // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   // return NO to not change text

- (BOOL)textFieldShouldClear:(UITextField *)textField;               // called when clear button pressed. return NO to ignore (no notifications)
- (BOOL)textFieldShouldReturn:(UITextField *)textField;              // called when 'return' key pressed. return NO to ignore.

@end

2、如果某个类继承UITextField,需要在该类内部实现监听,可以按以下方法处理

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self)
    {
        [self addTarget:self action:@selector(onChange:) forControlEvents:UIControlEventEditingDidEnd];// 可在自定义selector处理
    }
    return self;
}



typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
    UIControlEventTouchDown           = 1 <<  0,      // on all touch downs
    UIControlEventTouchDownRepeat     = 1 <<  1,      // on multiple touchdowns (tap count > 1)
    UIControlEventTouchDragInside     = 1 <<  2,
    UIControlEventTouchDragOutside    = 1 <<  3,
    UIControlEventTouchDragEnter      = 1 <<  4,
    UIControlEventTouchDragExit       = 1 <<  5,
    UIControlEventTouchUpInside       = 1 <<  6,
    UIControlEventTouchUpOutside      = 1 <<  7,
    UIControlEventTouchCancel         = 1 <<  8,

    UIControlEventValueChanged        = 1 << 12,     // sliders, etc.

    UIControlEventEditingDidBegin     = 1 << 16,     // UITextField
    UIControlEventEditingChanged      = 1 << 17,
    UIControlEventEditingDidEnd       = 1 << 18,
    UIControlEventEditingDidEndOnExit = 1 << 19,     // 'return key' ending editing</span></strong>

    UIControlEventAllTouchEvents      = 0x00000FFF,  // for touch events
    UIControlEventAllEditingEvents    = 0x000F0000,  // for UITextField
    UIControlEventApplicationReserved = 0x0F000000,  // range available for application use
    UIControlEventSystemReserved      = 0xF0000000,  // range reserved for internal framework use
    UIControlEventAllEvents           = 0xFFFFFFFF
};









版权声明:本文为博主原创文章,未经博主允许不得转载。

UIView的Touch事件UIControlEvents详解

首先,UIControlEvents有这个几种: UIControlEventTouchDown           = 1 UIControlEventTouchDownRepeat ...

IOS之UITextField 禁止粘贴

overrides the canPerformAction:withSender: method to return NO for actions that you don't want to ...

iOS-监听UITextField值改变事件

在实际情况中我们有时候在界面输入时候需要让用户输入的数据与模型同步,那么可能我们就需要监听UITextField值改变事件,然后在响应的方法中将新的值同步到模型中。这次我们主要提出三种方案,其中第一种...

UITextField的UIControlEventValueChanged事件

关于UITextField的UIControlEventValueChanged事件无响应的问题 监听UITextField文本改变的事件不是使用这个枚举 而是使用 UIControlEventE...

iOS 监听textfield的输入

首先:[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEd...

UITextField 监听数值变化的三种方法

1.直接监听//终点站直接监听 [cell.toStationField addTarget:self action:@selector(toStationFieldEdentDidE...

iOS开发之-------监听textview编辑状态

前面一篇文章中写过监听textfield编辑状态,但是textfield继承自UIControl,textview继承自,所以监听状态的这个方法就不好用了 - (void)addTarget...

iOS 开发中你是否遇到这些经验问题(二)

iOS 开发中你是否遇到这些经验问题(二)

iOS _监听UITextField输入状态

#import "RootViewController.h"@interface RootViewController ()@end@implementation RootViewController...

iOS 监听textfield的输入

1:首先 [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEven...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:IOS之监听UITextField的编辑事件
举报原因:
原因补充:

(最多只允许输入30个字)