本文介绍了如何在iOS中正确地继承UITextField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道互联网上有很多关于这个问题的信息,但我被困住了。我有应用程序,我需要自定义我的 UITextFields 。例如,我需要在编辑开始时更改边框颜色,或者我需要限制我可以在不同文本字段中设置的字符数。我还需要在几个文本字段中的图像,这些字段必须在左侧缩进。为了做到这一切,我决定制作自定义的UITextField类(以子类UITextField)。我从 UITextField 创建了新类(它实现了< UITextFieldDelegate> )。在那个课程中,我使用 self.delegate = self (这在互联网上被广泛使用,人们说它有效)所以我可以实现 shouldChangeCharactersInRange textFieldShouldBeginEditing 在我的自定义类中。我的问题是,在这种配置中,我收到无限循环和App重启(参见关于那个)。这来自 self.delegate = self 。我理解在某些情况下我可以使用 observer 但在这种情况下我如何在我的课程中实现 shouldChangeCharactersInRange
如果我没有以这种方式实现我的类并将我的文本字段委托给我的视图控制器。我必须在我的视图控制器类中实现所有方法,在我看来这是非常难看的解决方案。

I know that there is a lot of info on the Internet about that question but I am stuck. I have app in which I need to customise my UITextFields. For example I need change border colour when edit starts or I need limitation of how many characters I can set in different text fields. I also need an image in several text fields which has to be indented on the left. To do all this I decided to make custom UITextField class (to subclass UITextField). I created new class from UITextField (which implements <UITextFieldDelegate>). In that class I use self.delegate = self (this is widely used on the internet and people say it is working) so I can implement shouldChangeCharactersInRange or textFieldShouldBeginEditing inside my custom class. My problem is that in this configuration I receive infinite loop and App restart (see my question about that). This comes from self.delegate = self. I understand that in some cases I can use observer but in that case how I can implement shouldChangeCharactersInRange inside my class?If I don't implement my class in that way and delegate my text field to my view controller. I have to implement all that methods in my view controller class which in my opinion is very ugly solution.

所以我的问题是如何正确实现UITextField子类?

So my question is how properly implement UITextField subclass?

P.S。我想我是以错误的方式做到的,但我无法弄清楚哪一个是正确的。

P.S. I suppose that I do it in the wrong way but I can not figure out which is the proper one.

编辑:

这是我的代码:

MyCustomTextField.h

@interface MyCustomTextField : UITextField

@property (nonatomic) int maxSymbols;
@property (nonatomic) int leftIndent;

@end

MyCustomTextField.m

@interface MyCustomTextField () <UITextFieldDelegate>

@end

@implementation MyCustomTextField
- (id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {

        self.delegate = self;

        self.clipsToBounds = YES;
        [self setLeftViewMode:UITextFieldViewModeAlways];

        UIImageView *imageView1 = [[UIImageView alloc]
                                   initWithFrame:CGRectMake(0, 0, 37, 20)];
        imageView1.image = [UIImage imageNamed:@"otp_back"];
        self.leftView = imageView1;

    }
    return self;
}

- (CGRect) leftViewRectForBounds:(CGRect)bounds {

    CGRect textRect = [super leftViewRectForBounds:bounds];
    textRect.origin.x = 5;
    return textRect;
}

这种检查最大长度的方法:

also this method for checking max length:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    // Restrict number of symbols in text field to "maxSymbols"
    NSUInteger oldLength = [textField.text length];
    NSUInteger replacementLength = [string length];
    NSUInteger rangeLength = range.length;

    NSUInteger newLength = oldLength - rangeLength + replacementLength;

    BOOL returnKey = [string rangeOfString: @"\n"].location != NSNotFound;

    return newLength <= (int)_maxSymbols || returnKey;
}

当我转到文本字段并开始从虚拟键盘输入模拟器我收到无限循环并退出BAD ACCESS。这很奇怪,因为如果键盘是数字或密码类型,或者我从Mac键盘输入我没有收到问题。

When I go to the text field and start to type from virtual keyboard of the simulator I receive infinite loop and exit with BAD ACCESS. It it very strange, because if the keyboard is of type numeric or password or if I type from Mac keyboard I did not receive the issue.

推荐答案

我不会在你的自定义UITextField中将委托设置为self,它不是很整洁。
我会做的只是拥有一个属性或版本的方法,你可以在接到代表电话时在你的控制器中调用。

I wouldn't set the delegate to self within your custom UITextField, it is not really neat.What i would do is simply have a property or a method for edition that you can call in your controller when you receive delegates call.

基本上在你的视图中控制器在某处:

Basically in your view controller somewhere :

- (void)viewDidLoad{
    ....
    self.textField = [[CustomTextField alloc] init];
    self.textField.delegate = self;

}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
     self.textField.editMode = YES;
}

在CustomTextField中

And in your CustomTextField

-(void)setEditMode:(BOOL)edit{
    if(edit){
        self.layer.borderColor=[[UIColor redColor]CGColor];
        self.layer.borderWidth= 1.0f;
    }
}

这篇关于如何在iOS中正确地继承UITextField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 06:00
查看更多