自定义UITextField委托设置为self启动无限循环 [英] Custom UITextField delegate set to self initiates infinite loop

查看:83
本文介绍了自定义UITextField委托设置为self启动无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写iPhone App,我需要自定义UITextField类。对于我的文本字段,我需要缩进,图像在文本和最大字符之前。出于这个原因,我创建了基于UITextField的自定义类。我的所有文本字段都将基于这个新类。我使用Google并搜索了Stackoverflow,我发现在像我这样的情况下我必须在 init self.delegate = self; $ c>所以我不需要在我的View Controller类中实现像 textFieldShouldBeginEditing textFieldShouldEndEditing 这样的方法。我的所有文本字段都将在故事板中创建,因此 initWithCoder 。因此,在输入1,2或3个符号后,我收到无限循环和App崩溃(此刻我使用模拟器)。有趣的是,对于数字键盘或密码键盘,没有这样的问题。此外,如果我在我的Mac键盘上而不是在模拟器上键入符号,则没有问题。我试图调试,但在崩溃期间它直接跳转到循环并存在错误。
如何克服这个问题?

I am writing iPhone App in which I need custom UITextField class. For my text fields I need indent, image inside before the text and max characters. For this reason I created custom class based on UITextField. All my text fields will be based on this new class. I use Google and searched Stackoverflow and I find that in cases like mine I have to use self.delegate = self; during the init so I don't need to implement methods like textFieldShouldBeginEditing or textFieldShouldEndEditing inside my View Controller class. All my text fields will be created in storyboard so initWithCoder. As a result I receive infinite loop and App crash (I use simulator at this moment) after typing 1, 2 or 3 symbols. It is interesting that for numeric keyboard or password keyboard there is no such issue. Also if I type symbols on my Mac keyboard instead on the simulator there is no problem. I tried to debug but during the crash it jumps directly into loop and exist with error. How to overcome this problem?

P.S。我问了问题当我收到无限循环并且有评论 self.delegate = self; 会导致这样的循环,但我看到这个语句被广泛使用。可能是我没有正确地做到这一点,但我无法弄清楚如何从UITextField中创建可重用的类。

P.S. I asked question when I receive infinite loop and there was comments that self.delegate = self; can cause such loop, but I see that this statement is widely used. May be I did not do this correctly, but I can not figure out how to make reusable class out of UITextField.

编辑:
这是我的代码:

Here is my code:

On inti我初始化并设置边框颜色:

On inti I initialize and set border color:

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

        self.layer.borderColor=[[UIColor blackColor] CGColor];
        self.delegate = self;
    }
    return self;
}

当我开始编辑时,我改变边框颜色并设置缩进(缩进设置将是移动到属性设置器中):

When I start edit I change border color and set indent (indent set will be moved in property setter):

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    // Change border of text field that is editing to orange
    textField.layer.masksToBounds=YES;
    textField.layer.borderColor=[[UIColor orangeColor] CGColor];
    textField.layer.borderWidth= 1.0f;

    UIView *spacerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _leftIndent, 10)];
    [self setLeftViewMode:UITextFieldViewModeAlways];
    [self setLeftView:spacerView];

    return YES;
}

完成编辑后我返回颜色:

On finish editing I return back color:

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{

    // Change border of text field that is edited to black
    textField.layer.masksToBounds=YES;
    textField.layer.borderColor=[[UIColor blackColor] CGColor];
    textField.layer.borderWidth= 1.0f;

    return YES;
}

在价值变化时,我检查最多字符:

And on value change I check max characters:

- (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;
}


推荐答案

我不知道是否这可以是实际答案,但我已经解决了下一个问题的答案如何在iOS中正确地继承了UITextField?(来自 justafinger )。我没有在我的自定义控件中进行分配 self.delegate = self; ,但我有从视图控制器中的委托调用的方法。

I don't know if this can be actual answer, but I have work around which is the answer of my next question How properly subclass UITextField in iOS? (from justafinger). I don't make assignment self.delegate = self; in my custom control, but I have methods that are called from delegates in view controller.

我希望这可以帮助像我一样有问题的人。

I hope this can help people with problem like mine.

这篇关于自定义UITextField委托设置为self启动无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆