Saturday, June 18, 2011

Better way to retrieve a label/button using tag...

I used to write codes like this to loop through all subViews to retrieve a specific label/button using a Tag - which is quite dumb...


     for (UIView *aView in [inWhichView subviews]) {
        if ([aView isKindOfClass:[UILabel class]]) {
            if (aView.tag==TAG_LABEL_DISPLAY_TITLE) {
                labelTarget = (UILabel *)aView;
            }
        }
      }

Later found a better option from this book The iPhone Developer's Cookbook 2nd Edition by Erica Sadun which surely made a huge difference.



    labelTarget = (UILabel *) [self.view viewWithTag:TAG_LABEL_DISPLAY_TITLE];



See, achieved same result in one line, no more silly looping....


0 comments:

Post a Comment