我有一个包含大约50个小textField的视图。 在编辑那些textField时,在某些时候,所有textField都成为第一响应者并调用委托 - textFieldDidBeginEditing。 正在为循环中的所有textField调用textFieldDidBeginEditing,并且应用程序是堆栈。 (还在textFieldDidBeginEditing之前调用keyboardDidShow通知) 我无法找到这个奇怪事件的特定触发器,它只是在大约一分钟的编辑之后发生的。它只发生在iPhone上,永远不会发生在iPad上。
响应者对象在循环中成为第一响应者的原因是什么?
我正在添加一些似乎相关的代码。 这些TextField背后的想法是每个人都可以携带一个字母。每当textField获得它的字母时,它就会重新签名firstResponder并让下一个TextField成为FirstResponder。
我对UITextFieldDelegate的实现:
-(void)textFieldDidBeginEditing:(UIOneLetterTextField *)iTextField {
lastTextField = iTextField;
if (iTextField.didPressed)
{
iTextField.didPressed = NO;
[self setDefaultArrow];
[self hideAllCellsFill];
}
[iTextField performSelector:@selector(selectAll:) withObject:iTextField afterDelay:0.0];
[self createInputAccessoryView];
[iTextField setInputAccessoryView:inputAccView];
CGRect stvRect = CGRectMake(iTextField.frame.origin.x*lastScaleFactor, iTextField.frame.origin.y*lastScaleFactor, iTextField.frame.size.width*lastScaleFactor, iTextField.frame.size.height*lastScaleFactor);
lastTextFrame.origin.x = stvRect.origin.x - scrollView.contentOffset.x;
lastTextFrame.origin.y = stvRect.origin.y- scrollView.contentOffset.y;
lastTextFrame.size.width = stvRect.size.width;
lastTextFrame.size.height = stvRect.size.height;
[scrollView scrollRectToVisible:stvRect animated:YES];
}
- (BOOL)textField:(UIOneLetterTextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSLog(@"Key Pressed %@", string);
[[multiplayersModel multiplayersModelSharedInstance] localKeyPressedOnLocation:textField.cellLoc withText:string];
NSLog(@"resignFirstResponder (%u,%u)", textField.cellLoc.locationX, textField.cellLoc.locationY);
[textField resignFirstResponder];
UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:textField action:@selector(pressed:)];
[textField addGestureRecognizer:ges];
[self gotoNextTextfield:textField.cellLoc];
return NO;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
UIOneLetterTextField* tf =(UIOneLetterTextField*)(textField);
NSLog(@"resignFirstResponder (%u,%u)", tf.cellLoc.locationX, tf.cellLoc.locationY);
[textField resignFirstResponder];
return NO;
}
我注册的键盘NSNotifications的实现:
// avoid text behind keyboard.
-(void) keyboardDidShow: (NSNotification *)notif
{
NSLog(@"keyboardDidShow");
self.navigationController.navigationBar.hidden = YES;
CGRect keyboardRect = [[[notif userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
UIWindow *window = [[[UIApplication sharedApplication] windows]objectAtIndex:0];
UIView *mainSubviewOfWindow = window.rootViewController.view;
CGRect keyboardFrameConverted = [mainSubviewOfWindow convertRect:keyboardRect fromView:window];
CGFloat buriedUnderKeboard;
if ([self isPhone])
{
buriedUnderKeboard = lastTextFrame.origin.y + lastTextFrame.size.height + 35.0 - keyboardFrameConverted.origin.y;
}
else{
buriedUnderKeboard = lastTextFrame.origin.y + lastTextFrame.size.height + 65.0 - keyboardFrameConverted.origin.y;
}
if (buriedUnderKeboard > 0)
{
CGPoint offset = {scrollView.contentOffset.x, scrollView.contentOffset.y+buriedUnderKeboard};
[scrollView setContentOffset:offset animated:YES];
}
}
-(void) keyboardWillHide: (NSNotification *)notif
{
self.navigationController.navigationBar.hidden = NO;
[self hideAllCellsFill];
}
UIOneLetterTextField的实现(继承自UITextField) - 一个代表单字母TestField的类(initCellCharachter:withContentScaleFactor:是指定的初始化器):
#import "UIOneLetterTextField.h"
@interface UIOneLetterTextField()
@property CGFloat myoriginx;
@property CGFloat myoriginy;
@property CGFloat mywidth;
@property CGFloat myheight;
@property (nonatomic, strong) UITapGestureRecognizer * tapGes;
@end
@implementation UIOneLetterTextField
@synthesize cellLoc;
@synthesize didPressed;
@synthesize myoriginx;
@synthesize myoriginy;
@synthesize mywidth;
@synthesize myheight;
@synthesize tapGes;
-(void)myAddGesture
{
NSLog(@"resignFirstResponder (%u,%u)", self.cellLoc.locationX, self.cellLoc.locationY);
[self resignFirstResponder];
[tapGes setEnabled:YES];
UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pressed:)];
[self addGestureRecognizer:ges];
}
-(void)internalInit
{
UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pressed:)];
[self addGestureRecognizer:ges];
tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pressed:)];
[self addGestureRecognizer:tapGes];
self.textAlignment = NSTextAlignmentCenter;
self.font = [UIFont systemFontOfSize:MIN(self.mywidth,self.myheight)*3/4];
self.textColor = [UIColor blueColor];
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[self setReturnKeyType:UIReturnKeyDone];
}
-(void)pressed:(id)sender
{
didPressed = YES;
NSLog(@"becomeFirstResponder (%u,%u)", self.cellLoc.locationX, self.cellLoc.locationY);
[self becomeFirstResponder];
}
- (id)initCellCharachter:(cellCharachter *)c
withContentScaleFactor:(CGFloat)csf{
didPressed = NO;
c.tlx/=csf;
c.tly/=csf;
c.blx/=csf;
c.bly/=csf;
c.trx/=csf;
c.try/=csf;
c.brx/=csf;
c.bry/=csf;
self.cellLoc = c.cellLoc;
CGFloat width = MIN(c.trx,c.brx) - MAX(c.tlx,c.blx);
CGFloat height = MIN(c.bly, c.bry) - MAX(c.try,c.tly);
CGRect rct = CGRectMake(MAX(c.tlx,c.blx),MAX(c.tly,c.try),width,height);
self = [super initWithFrame:rct];
if (self) {
myoriginx =MIN(c.tlx,c.blx);
myoriginy =MIN(c.tly,c.try);
mywidth =width;
myheight =height;
[self internalInit];
}
return self;
}
//Disabling copy|paste|select popup menu
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
return NO;
}
@end
答案 0 :(得分:1)
我注意到如果你将多个UITextField链接到firstFirstResponder,当你关闭偷走第一个响应者的最后一个文本字段时,第二个最后一个文本字段将再次收到第一个响应者状态,就好像它正在恢复第一个响应者状态一样。
不确定这是否对您有所帮助。