为什么resignFirstResponder在iOS 5.0中被调用两次,而在iOS4.2-4.3中只被调用一次?
我在 App Store 中有一个为 4.2 编译的应用程序,在 iOS 5.0 下运行时表现得很奇怪。我已经追踪到 resignFirstResponder
在 iOS 5.0 下被调用了两次,而在 iOS 4.2 和 4.0 下只被调用了一次。 4.3.
我从 UITextField
派生了一个类,我在其中重写了 resignFirstResponder
。请参阅下面的示例代码。
在 iOS 4.2 & 下4.3 我在控制台中看到以下内容:
textFieldShouldReturn
resignFirstResponder
textFieldDidEndEditing
在 iOS 5.0 下,我在控制台中看到以下内容:
textFieldShouldReturn
resignFirstResponder
resignFirstResponder
textFieldDidEndEditing
在设备和模拟器上运行代码给出了恒定的结果。我错过了什么还是这是一个错误?
iOS 5.0 堆栈跟踪
#0 -[BugTextField resignFirstResponder] (self=0x681b530, _cmd=0x3769b41) at /Users/.../BugTextField.m:14
#1 0x006c05a6 in -[UIFieldEditor resignFirstResponder] ()
#2 0x006374e3 in -[UIView(Hierarchy) _willMoveToWindow:] ()
#3 0x006362c2 in __UIViewWillBeRemovedFromSuperview ()
#4 0x006360d7 in -[UIView(Hierarchy) removeFromSuperview] ()
#5 0x006bfff7 in -[UIFieldEditor becomeFieldEditorForView:] ()
#6 0x006ae37b in -[UITextField _resignFirstResponder] ()
#7 0x006eb8d4 in -[UIResponder _finishResignFirstResponder] ()
#8 0x006eba20 in -[UIResponder resignFirstResponder] ()
#9 0x006ae249 in -[UITextField resignFirstResponder] ()
#10 0x00017f68 in -[BugTextField resignFirstResponder] (self=0x681b530, _cmd=0x3769b41) at /Users/.../BugTextField.m:16
#11 0x0001828f in -[BugTextFieldVC textFieldShouldReturn:] (self=0x6829750, _cmd=0x18c5b, textField=0x681b530) at /Users/.../BugTextFieldVC.m:40
BugTextField.h
#import <UIKit/UIKit.h>
@interface BugTextField : UITextField
@end
BugTextField.m
#import "BugTextField.h"
@implementation BugTextField
- (BOOL) resignFirstResponder
{
NSLog(@"resignFirstResponder");
return [super resignFirstResponder];
}
@end
BugTextFieldVC.h
#import <UIKit/UIKit.h>
@class BugTextField;
@interface BugTextFieldVC : UIViewController <UITextFieldDelegate> {
BugTextField *bugTextField;
}
@end
BugTextFieldVC.m
#import "BugTextFieldVC.h"
#import "BugTextField.h"
@implementation BugTextFieldVC
- (id) init
{
if ( !(self = [super init]) )
{
return self;
}
// One text field with 100 height keyboard
bugTextField = [[BugTextField alloc] initWithFrame:CGRectMake(10, 10, 300, 30)];
bugTextField.borderStyle = UITextBorderStyleRoundedRect;
bugTextField.delegate = self;
[self.view addSubview:bugTextField];
return self;
}
- (void) dealloc
{
[bugTextField release];
[super dealloc];
}
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"textFieldShouldReturn");
[textField resignFirstResponder];
return YES;
}
- (void) textFieldDidEndEditing:(UITextField *)textField
{
NSLog(@"textFieldDidEndEditing");
}
@end
I have a app in the App Store compiled for 4.2 that is acting odd when running under iOS 5.0. I have tracked it down to resignFirstResponder
being called twice under iOS 5.0 while it was only being called once under iOS 4.2 & 4.3.
I have a derived a class from UITextField
where I override resignFirstResponder
. See the sample code below.
Under iOS 4.2 & 4.3 I see the following in the console:
textFieldShouldReturn
resignFirstResponder
textFieldDidEndEditing
Under iOS 5.0 I see the following in the console:
textFieldShouldReturn
resignFirstResponder
resignFirstResponder
textFieldDidEndEditing
Running the code on the device and simulator are giving constant results. Am I missing something or is this a bug?
The iOS 5.0 stack trace
#0 -[BugTextField resignFirstResponder] (self=0x681b530, _cmd=0x3769b41) at /Users/.../BugTextField.m:14
#1 0x006c05a6 in -[UIFieldEditor resignFirstResponder] ()
#2 0x006374e3 in -[UIView(Hierarchy) _willMoveToWindow:] ()
#3 0x006362c2 in __UIViewWillBeRemovedFromSuperview ()
#4 0x006360d7 in -[UIView(Hierarchy) removeFromSuperview] ()
#5 0x006bfff7 in -[UIFieldEditor becomeFieldEditorForView:] ()
#6 0x006ae37b in -[UITextField _resignFirstResponder] ()
#7 0x006eb8d4 in -[UIResponder _finishResignFirstResponder] ()
#8 0x006eba20 in -[UIResponder resignFirstResponder] ()
#9 0x006ae249 in -[UITextField resignFirstResponder] ()
#10 0x00017f68 in -[BugTextField resignFirstResponder] (self=0x681b530, _cmd=0x3769b41) at /Users/.../BugTextField.m:16
#11 0x0001828f in -[BugTextFieldVC textFieldShouldReturn:] (self=0x6829750, _cmd=0x18c5b, textField=0x681b530) at /Users/.../BugTextFieldVC.m:40
BugTextField.h
#import <UIKit/UIKit.h>
@interface BugTextField : UITextField
@end
BugTextField.m
#import "BugTextField.h"
@implementation BugTextField
- (BOOL) resignFirstResponder
{
NSLog(@"resignFirstResponder");
return [super resignFirstResponder];
}
@end
BugTextFieldVC.h
#import <UIKit/UIKit.h>
@class BugTextField;
@interface BugTextFieldVC : UIViewController <UITextFieldDelegate> {
BugTextField *bugTextField;
}
@end
BugTextFieldVC.m
#import "BugTextFieldVC.h"
#import "BugTextField.h"
@implementation BugTextFieldVC
- (id) init
{
if ( !(self = [super init]) )
{
return self;
}
// One text field with 100 height keyboard
bugTextField = [[BugTextField alloc] initWithFrame:CGRectMake(10, 10, 300, 30)];
bugTextField.borderStyle = UITextBorderStyleRoundedRect;
bugTextField.delegate = self;
[self.view addSubview:bugTextField];
return self;
}
- (void) dealloc
{
[bugTextField release];
[super dealloc];
}
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"textFieldShouldReturn");
[textField resignFirstResponder];
return YES;
}
- (void) textFieldDidEndEditing:(UITextField *)textField
{
NSLog(@"textFieldDidEndEditing");
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是适用于 iOS 4.2、4.3 和 5.0 的解决方法,它既不是火箭科学,也不是性感的,但它会起作用,直到我更好地理解正在发生的事情(我做错了什么还是这是一个错误?) 。我将此作为错误报告给 Apple。
BugTextField.h
BugTextField.m
Below is a work around that works for iOS 4.2, 4.3, and 5.0 It is not rocket science nor sexy, but it will work until I have better understanding of what is going on (Am I doing something wrong or is this a bug?). I am reporting this as a bug to Apple.
BugTextField.h
BugTextField.m
一种解决方案是重写
canResignFirstResponder
。这只会被调用一次。问题是这与文档不一致 - https://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIResponder_Class/Reference/Reference.html - 所以你可能是对的:这是一个错误。
One solution is to override
canResignFirstResponder
instead. This will only get called once.The problem is that this disagrees with the docs -- https://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIResponder_Class/Reference/Reference.html -- so you're probably right: It's a bug.