本文介绍了self.delegate = 自我;这样做有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

self.delegate = self; 这样做有什么问题?正确的做法是什么?

self.delegate = self; what's wrong in doing that?and what is the correct way of doing it?

谢谢,尼尔.

代码:

(UITextField*)initWith:(id)sender:(float)X:(float)Y:(float)width:(float)hieght:(int)textFieldTag {
    if (self = [super initWithFrame:CGRectMake(X, Y,width, hieght)]) {
        finalText = [[NSMutableString alloc] initWithString:@""];
        senderObject = sender;
        self.textColor = [UIColor blackColor];
        self.font = [UIFont systemFontOfSize:17.0];
        self.backgroundColor = [UIColor whiteColor];
        self.autocorrectionType = UITextAutocorrectionTypeNo;
        self.keyboardType = UIKeyboardTypeDefault;
        self.returnKeyType = UIReturnKeyDone;
        self.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.tag = textFieldTag;
        self.delegate = self;
        [sender addSubview:self];
    }
    return self;
}

注意事项:这是一个文本字段,当我将委托设置为另一个对象(self.delegate = mainView)时,一切正常,但是我必须在mainView,我想把它们放在 self(我创建的一个 uiTextField 类)中.如果我设置 self.delegate = self,我会得到一个 textField 但键盘没有显示.

Notes: This is a text field, and when I am setting the delegate to another object (self.delegate = mainView) everything works fine, but then I will have to implement the delegate methods in mainView, and I would like to put them in self (a uiTextField class which I have created). If I am setting self.delegate = self, I do get a textField but the keyboard doesn't show up.

推荐答案

查看本帖

http://www.cocoabuilder.com/archive/cocoa/241465-iphone-why-can-a-uitextfield-be-its-own-delegate.html#241505

基本上,当您以自己为委托单击 UITextField 时冻结"的原因是 respondsToSelector 正在调用自身 -> 无限递归.

Basically, the reason for the "freeze" when you click on your UITextField with itself as a delegate is that respondsToSelector is calling itself -> infinite recursion.

UITextField 是独一无二的 AFAIK.您通常可以使用一个类作为它自己的委托,而没有特别的问题.对于 UITextField,您必须创建一个实际的委托(当然,它可以调用作为委托的 UITextField 上的方法.请注意避免保留循环,即使您使用的是 ARC).

UITextField is unique AFAIK. You can usually use a class as its own delegate with no particular problems. For UITextField you must create an actual delegate (that could, of course, call methods on the UITextField for which it's a delegate. Just be careful to avoid retain loops, even if you're using ARC).

这篇关于self.delegate = 自我;这样做有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:15