Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are a few ways you could match up your labels and your text views. One easy way is using tags.</p> <p>Every <code>UIView</code> has an integer <code>tag</code> property that you can use however you wish. You can set the tag in code, or in the View section of the Attributes inspector in your nib. You can also ask a view to search all its descendants (including itself) for a view with a particular tag, using the <code>viewWithTag:</code> method, but we won't use that.</p> <p>Here's what you do. In your nib, give each label a different tag. Let's say you give label A a tag of 1, and label B a tag of 2, and so on. Give each text view the same tag as the corresponding label. So the text view that goes with label A also gets tag 1, and the text view that goes with label B also gets tag 2, and so on.</p> <p>Then, in your <code>viewDidLoad</code> method, you can just sort the outlet collections by tag. Let's say the labels are in an outlet collection named <code>labels</code> and the text views are in an outlet collection named <code>textViews</code>. Here's how you sort them:</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; NSComparator compareTags = ^(id a, id b) { NSInteger aTag = [a tag]; NSInteger bTag = [b tag]; return aTag &lt; bTag ? NSOrderedDescending : aTag &gt; bTag ? NSOrderedAscending : NSOrderedSame; }; self.labels = [self.labels sortedArrayUsingComparator:compareTags]; self.textViews = [self.textViews sortedArrayUsingComparator:compareTags]; } </code></pre> <p>Now the <code>labels</code> array and the <code>textViews</code> array are matched up, so that the label at index 0 of <code>labels</code> goes with the text view at index 0 of <code>textViews</code>, and so on.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload