iPhone - how to select from a collection of UILabels?

爱⌒轻易说出口 提交于 2020-03-16 11:28:45

问题


I have a few UILabels, any one of which will update according to the index of an NSArray index they represent. I thought of selecting them by their tag

self.displayLabel.tag = myArray[index];

but that changes the tag value to whatever my array is holding at the moment

Using a dictionary for whatever tricks it offers instead of an NSArray doesn't help because i still have to select the correct matching label. This is the effect i want to achieve.

self.|mySelectedLabel|.text = myArray[index];

what should i put in |mySelectedLabel| to get the one i'm looking for?

I'm almost ashamed to ask at my reputation level, but this is stymie-ing me every search only turns up how to set Labels and change, not the process of selecting


回答1:


Assuming you have set the tags to the appropriate index to match your array indices you can use [self.view viewWithTag:index];




回答2:


Why are you not setting the tag with:

self.displayLabel.tag = index;

Also, you could just loop though an array of labels and find the right one:

for (UILabel *label : labelArray) {
    if (label.tag == index) {
        label.text = @"I found you!";
    }
}



回答3:


Rather than using tags you can refer to your specific textfields by reference:

// Create an array to hold your textfields
NSMutableArray *textFields = [NSMutableArray array]

// Create your textfields and add them to the array
UITextField *textField;
for (NSUInteger idx = 0: idx++; idx < numberOfTextFieldsYouWant) {
    textField = [UITextField alloc] initWithFrame:<whateverYouWant>];
    [textFields addObject:textField];
}

Since you are adding the objects to an array, rather than using the tag value 0, 1, 2... you can just access it by it's index in the array

So, for what you want to do you can just do:

textfields[index].text = myArray[index];

It's a lot cleaner, doesn't rely on magic tags, and you have an array of all your dynamic textfields that you can remove, or change in one place.

I think tags are vastly overused, and they aren't necessary in most cases.




回答4:


Just letting you know I reframed the problem and this eventually worked for me without having to use an array ( with endless experimenting, I sort of bumped into it so I don't know if it constitutes good technique )

the desired label corresponding to the bag weight ( one of a number possible ) displays the right update

- (IBAction)acceptWeight:(UIButton *)sender {
   int tempValue = (int) currentWeight;
  // current weight comes from a UISegementedController

   for (UILabel *labels in self.view.subviews) 
   {
      if (labels.tag == currentWeight) 
      {
         bags[tempValue]++;
         labels.text = [NSString stringWithFormat:@"%i",bags[tempValue]];
      }
   }
  totalKilo = totalKilo + (int)currentWeight;
  self.totalKilo.text = [NSString stringWithFormat:@"%d",totalKilo];
}


来源:https://stackoverflow.com/questions/22649867/iphone-how-to-select-from-a-collection-of-uilabels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!