IOS 7 UITextField resignFirstResponder BAD [英] IOS 7 UITextField resignFirstResponder BAD
问题描述
我在使用UItextField时遇到崩溃,在我的customCell中,当我resignFirstResponder文本字段时,但它不再可见(表格视图滚出窗口)。我仍然可以找到文本字段,指针继续可访问,它不是空,并且崩溃只发生在IOS7上,在IOS6上我没有这个问题。下面是一些代码:
Im getting a crash, when using a UItextField, inside my customCell, and when i resignFirstResponder the textfield, but its not visible anymore(the table view scrolled out of window). I still can find the textfield, the pointer continues accessible, it is no null, and the crash only occurs on IOS7, on IOS6 i dont have this problem. Heres some code :
textField
是一个全局变量。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row];
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[TableCell alloc] init];
if(indexPath.row == 0)
{
[textField setFrame:CGRectMake(15, 5, cell.frame.size.width-60, cell.frame.size.height)];
textField.textAlignment = NSTextAlignmentLeft;
[textField setBorderStyle:UITextBorderStyleNone];
textField.textColor = [UIColor blackColor];
textField.tag = indexPath.row;
textField.delegate = self;
textField.secureTextEntry = YES;
[textField setFont:[UIFont fontWithName:@"Arial-BoldMT" size:15]];
textField.textColor = [UIColor whiteColor];
textField.returnKeyType = UIReturnKeyDone;
[textField setAdjustsFontSizeToFitWidth:YES];
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Senha" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
[cell.contentView textField];
}
}
return cell;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
// NSLog(@"text field %@",textField);
// NSLog(@"tfield return: %d",textField.isFirstResponder);
[textField resignFirstResponder];
// [self.view endEditing:YES];
return NO;
}
推荐答案
我已经成功修复了在Apple的帮助下类似的崩溃bug。关键是 reuseIdentifer
。
I've successfully fixed a similar crash bug with the help of Apple. The key is the reuseIdentifer
.
报价来自 Vincent Gable 的邮件 Apple开发人员技术支持:
这是iOS 7中已知的行为更改
UITableView
,当不重复使用单元格时。
This is a known behavior change that happens in iOS 7 with
UITableView
, when cells are not reused.
此处的修复是为了确保您遵循正确的单元格重用。如果您不想重新使用 UITableViewCells
,那么建议您只需在 UIScrollView
。
The fix here is to make sure that you follow proper cell reuse. If you do not want to re-use UITableViewCells
, then it is recommended that you simply layout all your views inside a UIScrollView
.
要确保重复使用单元格,请确保将相同的字符串传递给 dequeueReusableCellWithIdentifier:
你使用 alloc / init
来传递给 reuseIdentifier:
来制作单元格。这个字符串不能为零。
To make sure cells are re-used, make sure you are passing the same string to dequeueReusableCellWithIdentifier:
that you pass to reuseIdentifier:
when using alloc/init
to make the cell. This string can not be nil.
所以我认为你应该确保你已经设置了 TableCell
的 reuseIdentifer
属性,其值已传递给 dequeueReusableCellWithIdentifier:
So I think you should make sure you've set TableCell
's reuseIdentifer
property with the same value you've passed to dequeueReusableCellWithIdentifier:
这篇关于IOS 7 UITextField resignFirstResponder BAD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!