Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Well,
i have 3 nested NSEnumeration loops, used to get the textfields of a custom cell, in a custom table in a custom view in a controller...

How can i change this code to make more readable and more optimizated?

Here the code:

- (void) textfieldsOperations:(APOperation)op
{
  __block int Valid = 0;
  __block NSArray *sub = _Table.subviews;
  __block MyCell *cell = nil;
  __block UIView *view = nil;
  __block NSMutableArray *ret = [NSMutableArray arrayWithCapacity:5];

  [sub enumerateObjectsUsingBlock:^(id c, NSUInteger index, BOOL *stop)
  {
    if ( [c isKindOfClass:[MyCell class]] ) {
      cell = c;
      [cell.subviews enumerateObjectsUsingBlock:^(id v, NSUInteger index, BOOL *stop)
      {
        if ( [v isKindOfClass:[UIView class]] ) {
          view = v;
          [view.subviews enumerateObjectsUsingBlock:^(id t, NSUInteger index, BOOL *stop)
          {
            if ( [t isKindOfClass:[MyTextField class]] )
            {
              MyTextField *txt = t;
              switch ( op )
              {
                case APOperationClear:
                  // something with "txt"
                  break;
                case APOperationEnableSearch:
                  // something with "valid"
                  break;
                case APOperationGetText:
                  // something with "txt"
                  break;
                case APOperationPos: {
                  // something with "txt"
                  break;
                }
              }
            }
          }];
        }
      }];
    }
  }];
  //[...]      
}

thanks.

share|improve this question
add comment

1 Answer

honestly: this code should be replaced completely.

you dont give any informations about how your MyCell and MyTextField look like, so I can just give you a general advice: Let MyTextField have a delegate it can call if a certain operation is triggered on it.

ie it could call textField:(MyTextField *)textField didTriggerOperation:(APOperation)op and the implementation would be similar to:

-(void)textField:(MyTextField *)textField didTriggerOperation:(APOperation)op
{
    if(textField == self.descriptionTextField){
        if(op){
            switch ( op )
            {
              case APOperationClear:
                // something with "txt"
                break;
              case APOperationEnableSearch:
                // something with "valid"
                break;
              case APOperationGetText:
                // something with "txt"
                break;
              case APOperationPos: {
                // something with "txt"
                break;
              }
            }
        }

    }
}

your textField would call is when the operation was triggered like

[self.delegate textField:self didTriggerOperation:operation];
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.