Setting a tag button with a variable

I have a json array with specific objects: first name, last name, unique identifier. I can parse them and save them in a dictionary, and also check them for NULL conditions.

I created a table in which I can display the names. In addition, I have one button on each line, and I marked them. I want that when I click the button, I can see the unique identifier retrieved from the server in my console. I need to pass this unique identifier into the following form, since using this unique identifier will help me display the view corresponding to this id on the server.

Click here to see what I have done so far.

I made some changes by adding this to my viewDidLoad method:

  for(NSDictionary *items in infos)
      {
          ....
          ....
         for(int i =0; i < len; i++)
          {
             ...
             ...
             ...


             for(NSDictionary *newItems in infoArray)
               {
                ...
                ...

                NSString *folks_IdRcv = [[items objectForKey:strr]objectForKey:@"folks_id"];
                NSLog(@"FolksID = %@", folks_IdRcv);

                NSString *folksIDString = [NSString stringWithFormat:@"%@", folks_IdRcv, nil];
                NSLog(@"folkid_display=%@", folksIDString);


                if((fName && lName && email_Id && number_id) && displayName && folksIDString != NULL)
                     {

                        ...
                        ...                            
                        [folksIDArray insertObject:folksIDString atIndex:ii];
                NSLog(@"FolksID String Name = %@", [folksIDArray objectAtIndex:(ii)]);
                ii++;
                                }
                              }
                           }
                        } 

NSMutableArray *nArray = [[NSMutableArray alloc]initWithCapacity:len];


for(int j =0; j<len ;j++)
   {

      ...
      ... 
      [nArray addObject:[folksIDArray objectAtIndex:j]];
      NSLog(@"FID Element=%@", nArray);
}

self.infos = mArray;   
[super viewDidLoad];
}

:

FID Element=(
400386528950544,
162622322666106,
643171434889706
)

, , . , , , , .

, .

+3
4

, id, .h . .

-(IBAction)buttonClicked1:(id *)sender{
  NSString *UIDString = [uidarray objectAtIndex:[sender tag]]; // uidarray is  unique id  array 
  NSLog(@"uid for particular button=%@", UIDString);// this will give you kid for button that you tapped or clicked.
}

, .

+2

... TableView.....

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
.............your another code........
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(0.0, 0.0, 14, 14);
    button.frame = frame;   // match the button size with the image size

    [button setBackgroundImage:image forState:UIControlStateNormal];

    // set the button target to this table view controller so we can interpret touch events and map that to a NSIndexSet
    [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;    
    return cell;
}

...

- (void)checkButtonTapped:(id)sender event:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:Yourtableview];
    NSIndexPath *indexPath = [Yourtableview indexPathForRowAtPoint: currentTouchPosition];
    if (indexPath != nil)
    {
        [self tableView: Yourtableview accessoryButtonTappedForRowWithIndexPath: indexPath];
    }
}

tableButtonTappedForRowWithIndexPath Delegate.

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{

       NSMutableDictionary *item = [self.arrTypes objectAtIndex:indexPath.row];//sel.arrTypes is Array Data..use your Data here and
        .......  do somthing here which you want  ......
}
+1

A tag is one of the ways that viewing a supervisor is. See below code (no tag required)

- (IBAction)clickedButton:(id)sender 
{

    UIButton *button = (UIButton *)sender;

    UITableViewCell *cell = (UITableViewCell *)button.superview;

    UITableView *tableView = (UITableView *)cell.superview;

    NSIndexPath *indexPath = [tableView indexPathForCell:cell];

     NSString* id = [Array objectAtIndex:indexPath.row];
}

and if you use a tag, make sure the tag must be unique.

+1
source

use this UIButton* btn.....

btn.tag. 

and when you click

UIButton *button = (UIButton*)sender;
thetatosendserver = button.tag
0
source

All Articles