self.delegate = self;这样做有什么问题? [英] self.delegate = self; what's wrong in doing that?

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

问题描述

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

备注:这是一个文本字段,当我将代理设置为另一个对象(self.delegate = mainView)时,一切都可以正常工作,但是我将不得不在mainView中实现委托方法,我想将它们放在self(一个我创建的uiTextField类中) )。如果我设置self.delegate = self,我会得到一个textField,但键盘不显示。

解决方案

查看此主题



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



基本上,当您单击UITextField作为委托时,冻结的原因是 respondToSelector 正在调用自身 - >无限递归。



UITextField是唯一的AFAIK。你通常可以使用一个类作为自己的委托,没有特别的问题。对于UITextField,您必须创建一个实际的委托(当然可以在UITextField上调用它的代理方法,只要注意避免保留循环,即使你使用ARC )。


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

Thanks, Nir.

Code:

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

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 wich I have created). If I am setting self.delegate = self, I do get a textField but the keyboard doesn't show up.

解决方案

See this thread

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

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 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 = self;这样做有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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