我有一個使用UITextFieldDelegate協議的iPad應用程序。我宣佈並實施完整的協議。領域調用此協議聲明如下:UITextFieldDelegate問題
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」運行時類型要麼爲零或NSCFString根據該:
NSLog(@"class = %@, value='%@'", [ str class ], str);
當我編輯字符串時,一切正常。
在iPad相同的日誌記錄語句返回:
class = UIFieldEditor, value='<UIFieldEditor: 0xb5f400; frame = (0 0; 640 640); text = 'Foobar'; opaque = NO; layer = <UIWebLayer: 0x113ba0>>'
「STR」的後續用途作爲一個NSString導致iPad上的崩潰,當我嘗試添加,更改或刪除文本。 qrange也是無效的(長度是一些巨大的數字)。
UIFieldEditor是我沒有使用的UIWebView.h的一部分。
在某種程度上改變協議成員的聲明會影響行爲。
由於我找不到任何其他人有這個問題,我必須假設我做錯了什麼。
任何想法?
謝謝!我會嘗試刪除addTarget。該控件僅在某些情況下淡入,因此開始使用alpha 0.0和userInteraction NO。保留可能是從調試中遺留下來的。只要視圖保留了控件,引用計數應該是1,我應該可以設置屬性,不是嗎? – ToddK 2011-04-30 13:06:15
在這個特定的情況下,你可能會被保存它的視圖保存,但正如我所說的,假設它被其他東西保留的版本之後使用引用的模式不是我推薦的。 – Anna 2011-04-30 14:13:14