iPhone: the most useful tag property

All objects that derive from <a target="_blank" href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/cl/UIView">UIView</a> inherit the useful properties <a target="_blank" href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW25">tag</a> This property is a real user-data (a "place" that the developer used for general purposes) of type <a target="_blank" href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/c_ref/NSInteger">NSInteger</a> so where we can store only numbers.

The uses to which they can obviously depends on the circumstances however, it is useful to identify a particular object to taking a common occurrence. For example, imagine you have two <a target="_blank" href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html">UIAlertView</a> respond to the same event:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/ / First alert
[ [ UIAlertView alloc ] initWithTitle : @ "Primo" message : @ "Primo Alert" delegate : self cancelButtonTitle : @ "OK" otherButtonTitles : nil , nil ] ; UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "First" message: @ "First Alert" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil];
; [Alert show];
; [Alert release];

/ / ...

/ / Second alert
[ [ UIAlertView alloc ] initWithTitle : @ "Secondo" message : @ "Secondo Alert" delegate : self cancelButtonTitle : @ "OK" otherButtonTitles : nil , nil ] ; UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Second" message: @ "Second Alert" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil];
; [Alert show];
; [Alert release];

/ / Event
/ / OK button Cliccandi of this function will be called Alter
void ) alertView : ( UIAlertView * ) alertView clickedButtonAtIndex : ( NSInteger ) buttonIndex { - (Void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) {buttonIndex
/ / Todo
}

Note: code examples in C + + you will find directions. This is not entirely correct because, as you know, the language is Objective-C. This is due to the fact that the plugins I use to view the source code does not support Objective-C and C + + is the one that best approaches him.

How to differentiate between the two alert? Precisely using the property tag. After creating the alert just enter:

1
2
3
4
5
6
7
8
9
10
11
12
13
/ / First alert
[ [ UIAlertView alloc ] initWithTitle : @ "Primo" message : @ "Primo Alert" delegate : self cancelButtonTitle : @ "OK" otherButtonTitles : nil , nil ] ; UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "First" message: @ "First Alert" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil];
1 ] ; // imposto il tag di questo Alert ad 1 [Alert SETTING: 1] / / set the tag of this Alert to 1
; [Alert show];
; [Alert release];

/ / ...

/ / Second alert
[ [ UIAlertView alloc ] initWithTitle : @ "Secondo" message : @ "Secondo Alert" delegate : self cancelButtonTitle : @ "OK" otherButtonTitles : nil , nil ] ; UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Second" message: @ "Second Alert" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil];
2 ] ; // imposto il tag di questo Alert ad 2 [Alert SETTING: 2] / / set the tag of this Alert to 2
; [Alert show];
; [Alert release];

Now modify the event in order to understand which Alert has been closed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/ / Event
/ / OK button Cliccandi of this function will be called Alter
void ) alertView : ( UIAlertView * ) alertView clickedButtonAtIndex : ( NSInteger ) buttonIndex { - (Void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) {buttonIndex
alertView.tag ) { switch (alertView.tag) {
: case 1:
/ / First Alert
break;
: case 2:
/ / According Alert
break;
default:
break;
}
}

Exact same technique can be used if we have a series of <a target="_blank" href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html">UIButton</a> run-time. For example:

1
2
3
4
5
6
7
8
9
10
unsigned int i = 0 ; i < 10 ; i ++ ) { for (unsigned int i = 0; i <10; i + +) {
[ [ UIButton buttonWithType : UIButtonTypeCustom ] initWithFrame : CGRectMake ( i * 20 , i * 20 , 20 , 20 ) ] ; UIButton * myButton = [[UIButton buttonWithType: UIButtonTypeCustom] initWithFrame: CGRectMake (i * 20, * 20, 20, 20)];
@ "But" forState : UIControlStateNormal ] ; [MyButton setTitle: @ "But," Forst: UIControlStateNormal];
i ] ; // imposto il tag [MyButton SETTING: i]; / / set the tag

/ / The same event for all
self action : @selector ( onTouchUpInside : ) forControlEvents : UIControlEventTouchUpInside ] ; [MyButton addTarget: self action: @ selector (onTouchUpInside:) forControlEvents: UIControlEventTouchUpInside];

mybutton ] ; [Self.view addSubview: myButton];
}

(dove eseguiamo un casting <a target="_blank" href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/cl/UIView">UIView</a> In the event onTouchUpInside recover from the sender (where we perform a casting <a target="_blank" href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/cl/UIView">UIView</a> the property <a target="_blank" href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW25">tag</a>

1
2
3
4
void ) onTouchUpInside : ( id ) sender { - (Void) onTouchUpInside: (id) sender {
( ( UIView * ) sender ) .tag; button_tag unsigned int = ((UIView *) sender). tags;
/ / Todo
}

2 comments to "iPhone: the very useful property tags"

  1. June 12, 2009 Andrea Leganza :

    . You should not abuse the properties tag (obviously this is not the case) when you are in situations where the view is much structure, so that x subview, as this list is maintained with a NSArray , this is scanned in order to find this tag .
    le subview ) If the search is performed by comparing the numerical tag sequentially (and I do not think that it is not done in another way, the maximum order internally to tag the subview )

    objectatindex is not really well but I put for simplicity to understand the semantics.

    1
    2
    3
    4
    5
    scanning cycle {subviews
    self.subviews.objectatindex ( i ) .tag == tagrichiesto ) { if (self.subviews.objectatindex (s). tagrichiesto == tag) {
    i ) ; self.subviews.objectatindex return (s);
    }
    }

    1) in the best case the tag is the first so we made ​​a comparison
    2) in a case against the median we have n
    3) in the worst case, it does not exist, we performed for each iteration x against unnecessary

    ipotizzando il caso peggiore del non trovato: avremo effettuato 100*10=1000 confronti inutili, per non parlare di invocazioni in cascata all'array subview e ai vari metodi di analisi). It is often used to access the table cells, and in this situation, where we have hundreds of rows with x subview per line that has the most waste (if you are 100 cells with each 10 subviews assuming the worst case of not found: we made 100 * 10 = 1000 comparisons useless, not to mention the array of invocations cascade subview and the various methods of analysis).

  2. June 12, 2009 Andrea Leganza :

    visto che confrontiamo gli indirizzi fisici dei due oggetti (più veloce di così non si può). I forgot ... in some situations it might be advisable to use a iboutlet if we use IB (Interface Builder) or have a set of properties associated with these objects, to which we can access directly from any method of our class, if the comparison is an object or another in this case can be done simply by using the operator == since we compare the physical addresses of the two objects (faster so you can not).
    per evitare di aggiungere variabili locali. In the example shown is fine of course, eventually making a comparison between "whole" within the switch , but I have seen many use / abuse of the tag to avoid adding local variables.

    good planning

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA