我在App Store中为4.2编译了一个在iOS 5.0下运行时表现奇怪的应用程序。我已将其跟踪到resignFirstResponder
在iOS 5.0下被调用两次,而它仅在iOS 4.2和iOS下被调用一次。 4.3。
我从UITextField
派生了一个类,我在其中覆盖resignFirstResponder
。请参阅下面的示例代码。
在iOS 4.2& 4.3我在控制台中看到以下内容:
textFieldShouldReturn
resignFirstResponder
textFieldDidEndEditing
在iOS 5.0下,我在控制台中看到以下内容:
textFieldShouldReturn
resignFirstResponder
resignFirstResponder
textFieldDidEndEditing
在设备和模拟器上运行代码会产生持续的结果。我错过了什么或者这是一个错误吗?
#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
#import <UIKit/UIKit.h>
@interface BugTextField : UITextField
@end
#import "BugTextField.h"
@implementation BugTextField
- (BOOL) resignFirstResponder
{
NSLog(@"resignFirstResponder");
return [super resignFirstResponder];
}
@end
#import <UIKit/UIKit.h>
@class BugTextField;
@interface BugTextFieldVC : UIViewController <UITextFieldDelegate> {
BugTextField *bugTextField;
}
@end
#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
答案 0 :(得分:1)
下面是一个适用于iOS 4.2,4.3和5.0的解决方案它不是火箭科学也不是性感的,但它会起作用,直到我更好地理解发生了什么(我做错了什么或是这个错误?)。我将此报告为Apple的错误。
#import <UIKit/UIKit.h>
@interface BugTextField : UITextField {
// Value used to ensure code in resignFirstResponder is executed by the
// first stack frame and not subsequent stack frames in iOS 5.0.
//
// In iOS 5.0, one call to resignFirstResponder results in a second call to
// resignFirstResponder. In iOS 4.2 & 4.3, one call to resignFirstResponder
// does not result in subsequent calls to resignFirstResponder.
NSUInteger resignFirstResponderCallDepth;
}
@end
#import "BugTextField.h"
@implementation BugTextField
- (BOOL) resignFirstResponder
{
if (0 == resignFirstResponderCallDepth++)
{
// ---------------------------------------------------------------------
// Code executed by first stack frame to call resignFirstResponder.
NSLog(@"resignFirstResponder");
}
// -------------------------------------------------------------------------
// Code executed by every stack frame to call resignFirstResponder.
BOOL rV = [super resignFirstResponder];
resignFirstResponderCallDepth--;
return rV;
}
@end
答案 1 :(得分:0)
一种解决方案是覆盖canResignFirstResponder
。这只会被调用一次。
问题在于,这与文档不一致 - https://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIResponder_Class/Reference/Reference.html - 所以你可能是对的:这是一个错误。