【发布时间】: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