Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在UIButton上传递int值单击UITableView内部_Iphone_Objective C_Uitableview_Uibutton - Fatal编程技术网

Iphone 如何在UIButton上传递int值单击UITableView内部

Iphone 如何在UIButton上传递int值单击UITableView内部,iphone,objective-c,uitableview,uibutton,Iphone,Objective C,Uitableview,Uibutton,因此,我有一个视图控制器,它有一个向用户显示字符串的UITableView。但是我需要一种方法让用户使用普通的UIButton按id选择这个对象,所以我将它添加为子视图,单击事件按预期工作 问题是-如何将int值传递给此单击事件?我尝试过使用button.tag或button.accessibilityhint等按钮的属性,但没有太大成功。专业人员如何传递这样的int值 同样重要的是,我可以在后面的过程中从int中获得实际的[x intValue]。不久前我想我可以用一个简单的 NSLog(@“

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

问题是-如何将int值传递给此单击事件?我尝试过使用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 
}

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

或者,您也可以将UIButton子类化,并更适当地存储标记值。我想一个理想主义者会告诉你,这就是答案,因为你正试图让它与你的黑客一起工作


希望这能有所帮助。

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

或者,您也可以将UIButton子类化,并更适当地存储标记值。我想一个理想主义者会告诉你,这就是答案,因为你正试图让它与你的黑客一起工作


希望对您有所帮助。

最终解决方案包括以下几点更改:

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

关于按钮重用的最后一点意见是——我还没有发现这方面的运行问题,但我对另一个解决方案感兴趣,它仍然允许我在每个表单元格中有一个自定义按钮。

最终解决方案包括一些更改:

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

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

我认为您不需要*如果使用intI,我认为您不需要*如果使用intyou say NSLog(@“实际选择的帽子是%d”,[obj.idValue intValue]);不起作用,所以可能您从未设置过。因为我对设置标签和从发件人处通过操作方法读取标签做了相同的事情,但对我来说,它起作用。您需要附件按钮和“选择”按钮吗按钮?当点击附件按钮时,它将调用accessoryButtonTappedForRowWithIndexPath,因此不需要标记。我使用附件按钮查看帽子的详细信息,选择只会将特定帽子添加到购物车中以便结账/etc@iPortable-好问题(我会验证这一点,但我很高兴知道其他人正在这样做)然后,正如pbcoder指出的那样,您最初的问题似乎是在action方法中声明了一个int*,而不仅仅是一个int。然而,我将标记设置为indexPath.row,而不是对象中的一个值(这样,如果需要,您可以访问对象的其他属性)。此外,您正在将按钮添加到单元格中,而不考虑单元格的重复使用。当您向上/向下滚动时,单元格(已包含按钮)将被重复使用,然后将在该单元格上添加一个新按钮。您说的是NSLog(@“实际选择的帽子是%d”,[obj.idValue intValue]);不起作用,因此可能您从未设置它。因为我在设置标记和从发件人的操作方法中读取标记时做了相同的事情,但对我来说它起作用。您需要附件按钮和“选择”按钮吗?当点击附件按钮时,它将调用accessoryButtonTappedForRowWithIndexPath,因此不需要