我目前正在嘗試使我正在開發的應用程序的代碼更高效,更易於閱讀。基本上這是從NSUserDefaults的玩家名稱中檢索一個數組,並用這些名稱填充6個文本框(標記爲6-11)。如果沒有現有的數組,它將使用另一組名稱。任何想法簡化這個代碼將不勝感激。減少/ if語句的長度
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
NSMutableArray *names = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"nameArray"]];
for (int i = 0; i <= 5; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
UITableViewCell *cell = [playerTable cellForRowAtIndexPath:indexPath];
for (UIView *view in cell.contentView.subviews) {
if ([view isKindOfClass:[UITextField class]]) {
UITextField *txtField = (UITextField *)view;
if (txtField.tag == 6) {
if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
txtField.text = [names objectAtIndex:0]; }
else {
txtField.text = @"Peter";
}
}
if (txtField.tag == 7) {
if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
txtField.text = [names objectAtIndex:1]; }
else {
txtField.text = @"Julia";
}
}
if (txtField.tag == 8) {
if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
txtField.text = [names objectAtIndex:2]; }
else {
txtField.text = @"Durgan";
}
}
if (txtField.tag == 9) {
if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
txtField.text = [names objectAtIndex:3]; }
else {
txtField.text = @"Bob";
}
}
if (txtField.tag == 10) {
if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
txtField.text = [names objectAtIndex:4]; }
else {
txtField.text = @"Iseland";
}
}
if (txtField.tag == 11) {
if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
txtField.text = [names objectAtIndex:5]; }
else {
txtField.text = @"Player";
}
}
}
}
}
[self saveNames];
}
在[http://codereview.stackexchange.com](http://codereview.stackexchange.com)上可能會更好地詢問此問題。 –
謝謝,我已經發布在那裏。 –