self.delegate = self; 这样做有什么不对?

Tig*_*ger 9 iphone delegates self uitextfield

self.delegate = self; 这样做有什么不对?这样做的正确方法是什么?

谢谢,Nir.

码:

(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;
} 
Run Code Online (Sandbox Code Playgroud)

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

Dan*_*ark 12

看到这个帖子

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

基本上,单击你自己作为委托的UITextField时"冻结"的原因respondsToSelector就是调用自身 - >无限递归.

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