gpt4 book ai didi

iphone - self.delegate = self;这样做有什么问题吗?

转载 作者:行者123 更新时间:2023-12-03 18:32:42 30 4
gpt4 key购买 nike

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

谢谢,尼尔。

代码:

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

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

最佳答案

查看此主题

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

基本上,当您单击 UITextField 并将其自身作为委托(delegate)时,“卡住”的原因是 respondsToSelector 正在调用自身 -> 无限递归。

UITextField 是独一无二的。您通常可以使用一个类作为其自己的委托(delegate),而不会出现任何特殊问题。对于 UITextField,您必须创建一个实际的委托(delegate)(当然,可以调用它作为委托(delegate)的 UITextField 上的方法。请小心避免保留循环,即使您使用 ARC)。

关于iphone - self.delegate = self;这样做有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1747777/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com