UITextField becomeFirstResponder 触发_resignFirstResponder
Posted
技术标签:
【中文标题】UITextField becomeFirstResponder 触发_resignFirstResponder【英文标题】:UITextField becomeFirstResponder triggers _resignFirstResponder 【发布时间】:2013-12-23 11:51:10 【问题描述】:我正在以编程方式创建一个UITextField
,将其添加到视图中,然后通过调用开始编辑
[textField becomeFirstResponder];
但问题是这个调用触发了对 textFieldDidEndEditing: 委托方法的调用。堆栈跟踪指向由 becomeFirstResponder 调用的[textField _resignFirstResponder]
。这发生在模拟器和ios 7
上。我该如何避免这种情况?这导致了很多问题,因为我不希望在没有键盘被解雇的情况下调用textFieldDidEndEditing:
。
编辑:这是我创建 UITextField
的方法。
UITextField *titleField = [[[UITextField alloc] init] autorelease];
[titleField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
titleField.textColor = [UIColor colorWithWhite:1.f alpha:0.8];
titleField.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:40.f];
titleField.textAlignment = NSTextAlignmentCenter;
titleField.borderStyle = UITextBorderStyleBezel;
titleField.autocorrectionType = UITextAutocorrectionTypeNo;
// titleField.borderStyle = UITextBorderStyleLine;
titleField.frame = CGRectMake(0, 0, 200, 80);
titleField.center = self.previewView.center;
titleField.delegate = self;
NSDictionary *titleDictionary = [self titleDictionary];
if (titleDictionary)
titleField.text = [titleDictionary objectForKey:kTitleStringKey];
NSString *fontName = [titleDictionary objectForKey:kTitleFontNameKey];
CGFloat fontSize = [[titleDictionary objectForKey:kTitleFontSizeKey] floatValue];
titleField.font = [UIFont fontWithName:fontName size:fontSize];
NSData *textColorData = (NSData *)[titleDictionary objectForKey:kTitleTextColorKey];
titleField.textColor = [NSKeyedUnarchiver unarchiveObjectWithData:textColorData];
CGSize size = [titleField.text sizeWithFont:titleField.font];
titleField.frame = CGRectMake(0, 0, size.width + 40, 80);
titleField.center = self.previewView.center;
[self.view addSubview:titleField];
[titleField becomeFirstResponder];
【问题讨论】:
给我看看你创建 UITextField 的代码 请查看问题中的编辑。谢谢! 【参考方案1】:好的,我找到了。我有一个触发添加 UITextField 例程的 UILongPressGestureRecognizer,并且在长按时偶然调用了两次(需要弄清楚原因,但这是一个单独的问题)。所以 becomeFirstResponder 被调用了两次。
【讨论】:
以上是关于UITextField becomeFirstResponder 触发_resignFirstResponder的主要内容,如果未能解决你的问题,请参考以下文章
如何在 UITextField 子类中设置 UITextField 的属性
在 UITextField 操作中将 UITextField 字符串转换为 Double
如何调整 UITextField 占位符文本以适合 UITextField 宽度?