Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3672582
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:37:37+00:00 2026-05-19T02:37:37+00:00

So I have a view controller with a single UITableView that shows a string

  • 0

So I have a view controller with a single UITableView that shows a string to the user. But I needed a way to have the user select this object by id using a normal UIButton so I added it as a subview and the click event works as expected.

The issue is this – how can I pass an int value to this click event? I’ve tried using attributes of the button like button.tag or button.accessibilityhint without much success. How do the professionals pass an int value like this?

Also it’s important that I can get the actual [x intValue] from the int later in the process. A while back I thought I could do this with a simple

NSLog(@”the actual selected hat was %d”, [obj.idValue intValue]);

But this doesn’t appear to work (any idea how I can pull the actual int value directly from the variable?).

The working code I have is below

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

  if ([self.hats count] > 0) {
      Hat* obj = [self.hats objectAtIndex: [indexPath row]];

    NSMutableString* fullName = [[NSMutableString alloc] init];
    [fullName appendFormat:@"%@", obj.name];
    [fullName appendFormat:@" %@", obj.type];

    cell.textLabel.text = fullName;
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

    //add the button to subview hack
    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(50, 5, 50, 25);
    [button setTitle:@"Select" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor clearColor];
    button.adjustsImageWhenHighlighted = YES;

    button.tag = obj.idValue; //this is my current hack to add the id but no dice :(

    [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];

    [cell.contentView addSubview:button];
    //hack
  }

    return cell;  
}

- (void)action:(id)sender {
  int* hatId = ((UIButton *)sender).tag; //try to pull the id value I added above ...

  //do something w/ the hatId 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:37:37+00:00Added an answer on May 19, 2026 at 2:37 am

    The final solution consisted of a few changes:

    1.) Instead of using type int I switched to NSNumber (thanks to a few of the comments made)
    also I found int to be more of a problem when it comes to memory management because I’m new 🙁

    2.) Instead of setting the tag to an id I used some concepts mentioned in the comments and pushed the entire object to get more detail about it when I pulled it from action later.

    Here is the revised method where I set the object

      if ([self.hats count] > 0) {
          Hat* obj = [self.hats objectAtIndex: [indexPath row]];
    
        NSMutableString* fullName = [[NSMutableString alloc] init];
        [fullName appendFormat:@"%@", obj.name];
        [fullName appendFormat:@" %@", obj.type];
    
        cell.textLabel.text = fullName;
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    
        //add the button to subview hack
        UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(50, 5, 50, 25);
        [button setTitle:@"Select" forState:UIControlStateNormal];
        button.backgroundColor = [UIColor clearColor];
        button.adjustsImageWhenHighlighted = YES;
    
        button.tag = obj;
    
        [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    
        [cell.contentView addSubview:button];
        //hack
      }
    
        return cell;  
    }
    

    And here is the revised action where I pull out the object, then any property I want

    - (void)action:(id)sender {
      Hat* obj = ((UIButton *)sender).tag;
      NSNumber* hatId = obj.idValue;
      //then if I wanted the actual value of hatId I could do
      NSLog(@"print the hatId out as is %d", [hatId intValue]);
    }
    

    One last comment about the button reuse mentioned – I’m yet to find a running problem with this but I am interested in another solution that would still allow me to have a custom button in each table cell.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view user control that can post form. This control can be
I have a view that contains a UITableView and a UILabel which works perfectly
Lets say I have a view, myView, a view controller, myViewController, and some sort
A simple question: I have a Model-View-Controller setup, with Models accessing a SQL database.
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
When submitting one form from view, how can i also read or have controller
I have a viewController (Planner) that loads two view controllers (InfoEditor and MonthlyPlan) when
I have a view that has a list of jobs in it, with data
I have a view using a master page that contains some javascript that needs
We have a View (call it X) that is the base view called by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.