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 } |






Latest Comments
Mark : @ Mark: Thanks for the answer. For example, if you have a code to save / use the folder ...
Mark : Thanks for the answer. For example, if you have a code to save / use the folder ...
Giovambattista Fazioli : @ Mark: in principle, absolutely, a lot depends on how and what to download. La. ..
Mark : Hello, good guide! I wanted to ask, you can use the same code to run a sql database? Instead of ...
Joseph : Hi I wanted to ask you how you can do in the post I uploaded a gallery of 50 images, for example, ...