Monday, December 28, 2009

Tag Attribute

Tag is a property of UIView objects, includeing views an controls. It's a number assigned to any UIView object, and will keep no change in you application.

For example, if you have more than one object, which trigger the same action method, how can you identify the actual source of the event? Tag is a good solution. As shown in my previouse exercise "Change UIView.backgroundColor using UIColor".

 UISlider *sliderRed = (UISlider *)[self.view viewWithTag:10];
 UISlider *sliderGreen = (UISlider *)[self.view viewWithTag:11];
 UISlider *sliderBlue = (UISlider *)[self.view viewWithTag:12];





Assign Tag using Interface Builder

In Interface Builder with the UIView object select, there is a Tag property in the Inspector pane, as shown in the figure. You can enter the value you want. And also you can check this Tag value in your code to identify the object.