我有一个文本字段和一个委托方法shouldChangeCharactersInRange
。在这种方法中,a有一个自定义逻辑。
我想以编程方式设置textField.text
,以及如何调用shouldChangeCharactersInRange
我该怎么做?
默认情况下,设置textField.text
方法shouldChangeCharactersInRange
后未调用
答案 0 :(得分:0)
添加" UITextFieldDelegate"到你的班级并设置:
textField.delegate = self;
#import "ViewController.h"
@interface ViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.textField.delegate = self;
self.textField.text = @"Hello World";
[self textField:self.textField shouldChangeCharactersInRange:NSMakeRange(0, self.textField.text.length-1) replacementString:self.textField.text];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}
@end