2

I want to develop passcode like functionality in my app.For that i am using textfields.Entering passocode is done successfully.Now i want to implement clear passcode text entered on back button action of numberpad. Here is my code snippet of declaring protocol

@objc protocol BackPressDelegate {

func backPressed(info: NSDictionary)
}

class CustomTextField: UITextField, UIKeyInput {

var del1:BackPressDelegate?

override func deleteBackward() {

        super.deleteBackward()

        if ((self.delegate?.respondsToSelector("backPressed")) != nil) {

            self.del1?.backPressed(["tag":self.tag])
        }
    }
}

For doing this, i have created subclass of uitextfield in which i have override 'deleteBackward()' method.i have also set delegate of uitextfield in storyboard . My problem with this is deleteBackward is not getting called due to some reason when i press back button on numberpad.

rakeshbs
  • 24,392
  • 7
  • 73
  • 63
MURR
  • 91
  • 1
  • 4

1 Answers1

1

Try this subClass:

// customTextField.h
#import <UIKit/UIKit.h>
@protocol customTextFieldDelegate <NSObject>
@optional
- (void)TextFieldWillDelete:(TextField*)textField;
@end

// customTextField.m
#import "customTextField.h"
@implementation customTextField

- (void)deleteBackward {
    if([[[UIDevice currentDevice] systemVersion] intValue] < 8) {
        [super deleteBackward];
    }
    if ([_deleteDelegate respondsToSelector:@selector(TextFieldWillDelete:)]){
        [_deleteDelegate TextFieldWillDelete:self];
    }
}

- (BOOL)keyboardInputShouldDelete:(TextField *)textField {
    BOOL shouldDelete = YES;

    if ([TextField instancesRespondToSelector:_cmd]) {
        BOOL (*keyboardInputShouldDelete)(id, SEL, UITextField *) = (BOOL (*)(id, SEL, UITextField *))[UITextField instanceMethodForSelector:_cmd];

        if (keyboardInputShouldDelete) {
            shouldDelete = keyboardInputShouldDelete(self, _cmd, textField);
        }
    }

    if ([[[UIDevice currentDevice] systemVersion] intValue] >= 8) {
        [self deleteBackward];
    }

    return shouldDelete;
}

// yourViewController.m
@interface yourViewController () <customTextFieldDelegate>
@end
@implementation
-(void)TextFieldWillDelete:(TextField*)textField {
    NSLog(@"will delete: %@", textField.text);
}
@end

The same as UITextView.

Allen
  • 6,745
  • 5
  • 41
  • 59