【问题标题】:UIButton - Set property and use it when button is pressedUIButton - 设置属性并在按下按钮时使用它
【发布时间】:2014-10-20 17:32:40
【问题描述】:

我有一个名为 Cell Button 的 UIButton,它位于我的 .h 文件中

@interface CellButton : UIButton

@property (nonatomic,retain) CellButton *property;

在这个单元格中,我有 2 个相同类的按钮,然后我想设置 btn1.property = 0;btn2.property = 1; 我在单元格中将其设置为 indexPath 的行。

然后我在按下按钮时调用它:

[cell.Btn1 addTarget:self action:@selector(BtnClicked:) forControlEvents:UIControlEventTouchUpInside];
 [cell.Btn2 addTarget:self action:@selector(BtnClicked:) forControlEvents:UIControlEventTouchUpInside];

方法如下:

-(void)BtnClicked:(UIButton*)sender{
    DiningHallTableViewController *dtvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DiningHallTableViewController"];
[self.navigationController pushViewController:dtvc animated:YES];

    //depending on what button is tapped I want to send the number of the button pressed
}

我怎样才能让这个发送号码?

编辑

这是在我的 cellForRowAtIndexPath 中

在我的方法 BtnClicked 中我有

 NSIndexPath *myIP = [NSIndexPath indexPathForRow:sender.tag inSection:0];

TwoMealsTableViewCell *cell = (TwoMealsTableViewCell*)[self.tableView cellForRowAtIndexPath:myIP];

【问题讨论】:

  • 问题是什么?您的属性应键入 int 而不是 CellButton (@property (nonatomic) int property;)。目前尚不清楚您是否需要对按钮进行子类化才能执行此操作。您是否将按钮的标签设置为 indexPath.row?
  • @rdelmar 是的,该标签已被用于此目的。我只想为单元格中的按钮添加一个属性。所以我知道它是按下的第一个 btn 还是按下的第二个按钮。基本上我想知道在被调用的方法中按下了什么按钮。
  • 如果需要,您可以通过检查按钮的标题来做到这一点。那么你就不需要子类化按钮了。
  • 您还可以在每个按钮上设置一个标签。 button1.tag = 0; button2.tag = 1; 在 cellForRow:atIndexPath: 中,然后在回调方法中获取sender.tag
  • 在这个任务中使用子类按钮实在是太过分了。您可以使用按钮的标签对多于一项的信息进行编码。例如,您可以给 firstMealButton 一个 indexPath.row 的标签,给 secondMealButton 一个 indexPath.row +10 的标签(我假设这是针对在表中有 7 行的同一个应用程序)。在 button 方法中,tag/10 会给你 0 或 1,tag % 10 会给你余数,即 indexPath.row。

标签: ios objective-c uibutton


【解决方案1】:
-(void)tableView:(UITableView *)table cellForRowAtIndexPath(IndexPath *):indexPath
{
      // Assuming one section, if not then just take section number into account when getting cell
      // You should be doing this part already
      cell = self.contentArray[indexPath.row];
      // Set tags on the buttons
      cell.btn1 = [[CellButton alloc] init];
      cell.btn2 = [[CellButton alloc] init];
      cell.btn1.property = 0;
      cell.btn2.property = 1;

      [cell.btn1 addTarget:self action:@selector(BtnClicked:)forControlEvents:UIControlEventTouchUpInside];
      [cell.btn2 addTarget:self action:@selector(BtnClicked:) forControlEvents:UIControlEventTouchUpInside];

      // do anything else you need to do with the cells

      return cell
}

-(void)BtnClicked:(UIButton *)sender{
    DiningHallTableViewController *dtvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DiningHallTableViewController"];
[self.navigationController pushViewController:dtvc animated:YES];

    //depending on what button is tapped I want to send the number of the button pressed
    if ([sender isKindOfClass[CellButton class]]) {
        property = ((CellButton *)sender).property;
    }

    // Now do whatever you need to do with the number
}

我还应该补充一点,假设您正在为 iOS 5+ 开发,您应该通过故事板 segues 进行导航,而不是创建新的 VC 并将其推入堆栈。网上有很多使用故事板转场的教程,such as this one

第三次编辑

从截图中可以看出你的问题出在代码中:

@interface CellButton : UIButton

@property (nonatomic,retain) CellButton *property;

当您确实希望它是整数时,您将属性的类型设置为 CellButton。将上面的行更改为

@interface CellButton : UIButton

@property (nonatomic) NSInteger property;

而且您进行导航的方式不符合当前的最佳做法。虽然如果你不使用故事板(我不明白你为什么不这样做)我想你的解决方案是可行的。

【讨论】:

  • 标签属性已经被使用了,像我这样推送视图控制器有什么问题?我已为问题添加了屏幕截图
  • 我没有 contentArray 那应该是什么cell = self.contentArray[indexPath.row];
  • self.contentArray 是您保存 tableView 内容的任何结构。我只是选择了一个通用名称
  • 当我把它改成我得到并且错误说property with 'retain (or strong)' attribute must be of type object
  • 在定义属性时将保留从括号中取出。 NSInteger 不是对象,因此 retain 不适用于它。
【解决方案2】:

您收到该特定错误的原因是这一行:

@property (nonatomic,retain) CellButton *property;

您将property 声明为CellButton 类型(在CellButton 类中,这是自引用的,如果它有效,我会感到惊讶)。

当您在cellForRowAtIndexPath 中设置property 时,您将其设置为01,它们属于基本类型int,而不是CellButton 类型 - 因此会出现错误。

要修复错误,请按照 rfj001 的说明操作并更改您的 @property 声明。

@property (nonatomic, retain) NSInteger property;

另外,请考虑使用 ARC。不再需要弄乱/保留计数和手动内存管理。

【讨论】:

    猜你喜欢
    • 2013-06-16
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多