如何在UITutView上单击UITutView中传递一个int值

时间:2010-12-31 14:43:13

标签: iphone objective-c uitableview uibutton

所以我有一个带有单个UITableView的视图控制器,它向用户显示一个字符串。但是我需要一种方法让用户使用普通的UIButton通过id选择这个对象,所以我将它添加为子视图,click事件按预期工作。

问题是这样的 - 如何将int值传递给此click事件?我已经尝试使用按钮的属性,如button.tag或button.accessibilityhint,但没有取得多大成功。专业人士如何传递这样的int值?

同样重要的是我可以在稍后的过程中从int获取实际的[x intValue]。前一阵子我觉得我可以用一个简单的

做到这一点

NSLog(@“实际选择的帽子是%d”,[obj.idValue intValue]);

但这似乎不起作用(我知道如何直接从变量中提取实际的int值?)。

我的工作代码在

之下
- (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 
}

3 个答案:

答案 0 :(得分:2)

NSInteger hatId = ((UIButton *)sender).tag;

答案 1 :(得分:1)

我会以完全不同的方式解决这个问题。我会有一个NSMutableDictionary包含按钮对象,标签为键。可能在表的数据源中。然后,当该按钮或标签发生任何事情时,您可以轻松地进入其中一个或另一个。然后,当您需要给定按钮的标记时,您可以在字典上使用allKeysForObject方法,该方法可能只有一个对象。如果需要标记按钮,可以使用标记作为键值来获取按钮指针。这假设标签值不会被重用并且是唯一的(如果它们是你必须在按钮消失时将它们从字典中删除)。

或者,您也可以子类化UIButton并更适当地存储标记值。我认为理想主义者会告诉你这就是答案,就你如何使它与你的黑客一起工作而言。 =)

希望有所帮助。

答案 2 :(得分:0)

最终解决方案包括一些变化:

1。)而不是使用类型int我切换到NSNumber(感谢一些评论)   我也发现int在内存管理方面更成问题,因为我是新手:(

2.。)我没有将标签设置为id,而是使用了评论中提到的一些概念,并推动了整个对象,以便在我稍后从行动中提取它时获得更多关于它的细节。

以下是我设置对象

的修订方法
  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;  
}

这是修改后的行动,我将对象拉出来,然后是我想要的任何属性

- (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]);
}

关于提到的按钮重用的最后一条评论 - 我还没有找到一个运行问题,但我对另一个仍然允许我在每个表格单元格中有一个自定义按钮的解决方案感兴趣。