zoukankan      html  css  js  c++  java
  • Detect backspace in UITextField

    Is there any way to detect when the backspace/delete key is pressed in the iPhone keyboard on a UITextField that is empty?
    Here is non-hacky and very simple with a quick subclass of UITextField.
    This is the best solution. Objective C is based on sub classing and this solution uses it properly to solve the problem.
    Sadly, this only works for >= iOS 6 though

    //Header
    //MyTextField.h

    //create delegate protocol
    @protocol MyTextFieldDelegate <NSObject>
    @optional
    - (void)textFieldDidDelete;
    @end

    @interface MyTextField : UITextField<UIKeyInput>

    //create "myDelegate"
    @property (nonatomic, assign) id<MyTextFieldDelegate> myDelegate;
    @end

    //Implementation
    #import "MyTextField.h"

    @implementation MyTextField

    - (void)deleteBackward {
        [super deleteBackward];

        if ([_myDelegate respondsToSelector:@selector(textFieldDidDelete)]) [_myDelegate textFieldDidDelete];
    }

    @end

    //View Controller Header
    #import "MyTextField.h"

    //add "MyTextFieldDelegate" to you view controller
    @interface ViewController : UIViewController <MyTextFieldDelegate>
    @end


    Now simply add "MyTextFieldDelegate" to your ViewController and set your textfields "myDelegate" to "self":

    //View Controller Implementation
    - (void)viewDidLoad {
        //initialize your text field
        MyTextField *input = [[MyTextField alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];

        //set your view controller as "myDelegate"
        input.myDelegate = self;

        //add your text field to the view
        [self.view addSubview:input];
    }

    //MyTextField Delegate
    - (void)textFieldDidDelete {
        NSLog(@"delete");
    }

  • 相关阅读:
    Android JNI 本地开发接口
    Android 主题切换 小结
    Android 屏幕适配
    android 中获取视频文件的缩略图(非原创)
    android 多媒体数据库(非原创)
    Android tween 动画 XML 梳理
    activity 四种启动模式
    Activity 横竖屏切换
    Android Activity 管理 (AppManager)(非原创)
    Android moveTaskToBack(booleannon Root)
  • 原文地址:https://www.cnblogs.com/nanoCramer/p/3290181.html
Copyright © 2011-2022 走看看