UITextFieldDelegate问题 [英] UITextFieldDelegate problem
问题描述
我有一个使用UITextFieldDelegate协议的iPad应用程序。我宣布并实施完整的协议。调用此协议的字段声明如下:
I have an iPad app using the UITextFieldDelegate protocol. I declare and implement the full protocol. The field calling this protocol is declared as follows:
typingInput = [ [ [ UITextField alloc ] initWithFrame: textIn ] retain ];
dp.x = center.x;
dp.y = center.y - 42;
typingInput.center = dp;
[ self addSubview: typingInput ];
[ typingInput release ];
typingInput.font = [ UIFont systemFontOfSize: 52.0 ];
[ typingInput setTextColor: [ UIColor whiteColor ] ];
typingInput.backgroundColor = [ UIColor colorWithWhite: 0.0 alpha: 0.0 ];
typingInput.alpha = 0.0;
typingInput.userInteractionEnabled = NO;
[ typingInput addTarget: self action: @selector(textField:shouldChangeCharactersInRange:replacementString:) forControlEvents: UIControlEventEditingChanged | UIControlEventAllEditingEvents ];
typingInput.text = @"";
typingInput.autocorrectionType = UITextAutocorrectionTypeNo;
typingInput.enablesReturnKeyAutomatically = YES;
typingInput.delegate = self;
问题在于
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange: (NSRange)qrange replacementString: (NSString *)str;
在模拟器上,'str'的运行时类型为nil或NSCFString,具体如下:
On the simulator the run-time type of 'str' either is nil or NSCFString according to this:
NSLog(@"class = %@, value='%@'", [ str class ], str );
编辑字符串时,一切正常。
Everything works fine when I edit the string.
在iPad上返回相同的日志语句:
On the iPad the same logging statement returns:
class = UIFieldEditor, value='<UIFieldEditor: 0xb5f400; frame = (0 0; 640 640); text = 'Foobar'; opaque = NO; layer = <UIWebLayer: 0x113ba0>>'
'str'作为NSString的后续使用原因我试图添加,更改或删除文本时在iPad上崩溃。 qrange也无效(长度是一些巨大的数字)。
Subsequent uses of 'str' as an NSString causes a crash on the iPad when I attempt to add, change or delete text. qrange is also invalid (length is some huge number).
UIFieldEditor是我不使用的UIWebView.h的一部分。
UIFieldEditor is part of UIWebView.h which I am not using.
在某种程度上,改变协议成员的声明会影响行为。
To some extent changing the declaration of the protocol members affects the behavior.
由于我找不到其他有这个问题的人我不得不假设我在做出错了。
Since I cannot find anyone else with this problem I have to assume I am doing something wrong.
任何想法?
推荐答案
shouldChangeCharactersInRange
委托方法将由UITextField自动调用(通过设置其委托
属性)。如果你已经实现了UITextField,它们也可以调用其他委托方法。
The shouldChangeCharactersInRange
delegate method will automatically be called by the UITextField (by having set its delegate
property). The UITextField may also call other delegate methods if you've implemented them.
你没有(也不应该)使用<$ c将委托方法显式连接到UITextField事件$ C> addTarget 。这将导致委托方法被调用两次(一次由协议定义,第二次由addTarget调用)。
You do not (and should not) explicitly connect delegate methods to a UITextField event using addTarget
. This will result in the delegate method getting called twice (once as defined by the protocol and second by your addTarget).
通过(错误地)将UIControl事件连接到该方法,使用与方法参数不匹配的参数调用它。 UIControl事件处理方法( shouldChangeCharactersInRange
不是)具有以下形式:
By (wrongly) connecting a UIControl event to that method, it gets called with parameters that don't match up with the method's parameters. UIControl event handling methods (which shouldChangeCharactersInRange
is not) have these forms:
- (void)methodName;
- (void)methodName:(id)sender;
- (void)methodName:(id)sender withEvent:(UIEvent *)event;
参见 Cocoa Target-Action 和 iPhone App Development Lecture 4 。
删除要修复的addTarget行。
另外:
Separately:
- 是不是将alpha设置为0.0使控件不可见?
- 为什么userInteractionEnabled设置为NO?
- 显式调用retain,调用addSubView,调用release,然后设置属性(即使它工作)不是通常的做事方式。删除保留并移动addSubView并释放到最后。
这篇关于UITextFieldDelegate问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!