From 778e00461952bef6dc79daae1f31ca975751d65d Mon Sep 17 00:00:00 2001 From: Tom Date: Fri, 6 Dec 2013 11:14:55 -0800 Subject: [PATCH] #733 - fix for crash when editing sites --- WordPress/Classes/EditSiteViewController.m | 35 ++++++++++++++-------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/WordPress/Classes/EditSiteViewController.m b/WordPress/Classes/EditSiteViewController.m index 180ee2d8fe0f..f9085fd6cb7b 100644 --- a/WordPress/Classes/EditSiteViewController.m +++ b/WordPress/Classes/EditSiteViewController.m @@ -340,19 +340,30 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField { } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { - UITableViewCell *cell = (UITableViewCell *)[textField superview]; - - if (NSClassFromString(@"UITableViewCellScrollView")) { - // iOS7 introduced a private class in between the normal UITableViewCell and the cell views. - cell = (UITableViewCell*)[cell superview]; - } - NSMutableString *result = [NSMutableString stringWithString:textField.text]; - [result replaceCharactersInRange:range withString:string]; - if ([result length] == 0) { - cell.textLabel.textColor = WRONG_FIELD_COLOR; - } else { - cell.textLabel.textColor = GOOD_FIELD_COLOR; + // Adjust the text color of the containing cell's textLabel if + // the entered information is invalid. + if ([textField isDescendantOfView:self.tableView]) { + + UITableViewCell *cell = (id)[textField superview]; + while (![cell.class isSubclassOfClass:[UITableViewCell class]]) { + cell = (id)cell.superview; + + // This is a protection against a textfield not placed withing a + // table view cell + if ([cell.class isSubclassOfClass:[UITableView class]]) { + return YES; + } + } + + NSMutableString *result = [NSMutableString stringWithString:textField.text]; + [result replaceCharactersInRange:range withString:string]; + + if ([result length] == 0) { + cell.textLabel.textColor = WRONG_FIELD_COLOR; + } else { + cell.textLabel.textColor = GOOD_FIELD_COLOR; + } } return YES;