Wednesday 9 April 2014

How to assign Custom tag or value to UIButton or Any Component?

For Other Way to assign Custom object to Components: 
Please Refer this Post :
  1. http://labs.vectorform.com/2011/07/objective-c-associated-objects/ 
  2. http://nsscreencast.com/episodes/81-associated-objects

Here is a UIButton in below Code : 

UIButton *SubCatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
SubCatBtn.frame = CGRectMake(197, 9, 150, 50);
[SubCatBtn setTitle:@"Sample" forState:UIControlStateNormal];
SubCatBtn.titleLabel.textColor = [UIColor whiteColor];
SubCatBtn.titleLabel.font = [UIFont systemFontOfSize:16.5f];
SubCatBtn.titleLabel.numberOfLines = 0;
SubCatBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
SubCatBtn.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;[SubCatBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];


If You want a custom tag or property or attribute to this uibutton, then you can use the UIButton Layer Method for that.

You can use the layer property. Add the object you want to pass as the value in the layer dictionary.

[[SubCatBtn layer] setValue:@"jigar" forKey:@"name"];
 
 
This yourObj is accessible from the button action function:

-(void)btnClicked:(id)sender
{
    id yourObj = [[sender layer] valueForKey:@"name"];
}
 
 
With this method you can pass multiple values to the button function just by adding new objects in the dictionary with different keys.

No comments:

Post a Comment