简体   繁体   English

IOS 7 UITextField resignFirstResponder BAD

[英]IOS 7 UITextField resignFirstResponder BAD

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). 我在使用UItextField时遇到了崩溃,在我的customCell中,当我resignFirstResponder文本字段时,但它不再可见(表格视图滚出窗口)。 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. 我仍然可以找到文本字段,指针继续可访问,它不为空,崩溃只发生在IOS7上,在IOS6上我没有这个问题。 Heres some code : 下面是一些代码:

The textField is a global variable. 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;
}

I've successfully fixed a similar crash bug with the help of Apple. 我已经在Apple的帮助下成功修复了类似的崩溃错误。 The key is the reuseIdentifer . 关键是reuseIdentifer

The quote is from a mail from Vincent Gable of Apple Developer Technical Support : 报价来自Apple Developer Technical Support的 Vincent Gable发来的邮件:

This is a known behavior change that happens in iOS 7 with UITableView , when cells are not reused. 这是在iOS 7中使用UITableView发生的已知行为更改,此时不重用单元格。

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 . 如果您不想重复使用UITableViewCells ,那么建议您只需在UIScrollView布局所有视图。

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. 要确保重用单元格,请确保将相同的字符串传递给dequeueReusableCellWithIdentifier:传递给reuseIdentifier:使用alloc/init创建单元格时。 This string can not be nil. 这个字符串不能为零。

So I think you should make sure you've set TableCell 's reuseIdentifer property with the same value you've passed to dequeueReusableCellWithIdentifier: 所以我认为你应该确保你已经将TableCellreuseIdentifer属性设置为你传递给dequeueReusableCellWithIdentifier:的相同值dequeueReusableCellWithIdentifier:

You need to do some more research into how UITableViews work and reconsider your design. 您需要对UITableViews如何工作以及重新考虑您的设计进行更多研究。 Storing a UITextField in a global variable and trying to position it like this is not the right approach. 将UITextField存储在全局变量中并试图像这样定位它不是正确的方法。 Even if you could solve the immediate problem, which is likely that the UITextField has been released along with the UITableViewCell, this design is only going to get you into trouble further down the line. 即使您可以解决即时问题,这可能是UITextField与UITableViewCell一起发布的,但这种设计只会让您陷入困境。

Instead, consider subclassing UITableViewCell and adding a UITextField property to your subclass. 相反,请考虑继承UITableViewCell并将UITextField属性添加到子类。

You probably don't want to be using a different CellIdentifier for every single row either. 您可能不希望为每一行使用不同的CellIdentifier。

Maybe i've solved. 也许我已经解决了。 It's a little bit dirty methot but i think it work. 这是一个有点脏的methot但我认为它的工作。 I store all the cell that cellForRowAtIndexPath create 我存储了cellForRowAtIndexPath创建的所有单元格

if (!cell) { cell = [[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"FormCell_%@",cellID] owner:nil options:nil] lastObject]; [self.allTheCell addObject:cell]; } if (!cell) { cell = [[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"FormCell_%@",cellID] owner:nil options:nil] lastObject]; [self.allTheCell addObject:cell]; } the app doesn't crash anymore on ios7 if (!cell) { cell = [[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"FormCell_%@",cellID] owner:nil options:nil] lastObject]; [self.allTheCell addObject:cell]; }应用程序不崩溃了上ios7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM