gpt4 book ai didi

ios - 获取位于 UITableView 中的 UIButton 的标题

转载 作者:行者123 更新时间:2023-11-29 02:29:54 25 4
gpt4 key购买 nike

我得到了一个 UITableView,里面有 x 个 UITableViewCell。在我的单元格中,我有 3 个 UIButton(callHome、callMobile、sendEmail)。每个人都有一个 IBAction。我必须获取 UIButton 的标题并调用/发送邮件到标题中的数据。

我试图在索引 x 处捕获按钮的标题以与之交互,但我无法获取它。

最佳答案

如果您想在 UITableView 所在的 ViewController 中捕获按钮的目标操作,您可以使用 block 。 Block programming guide

您的自定义 UITableViewCell 类接口(interface)(.h 文件):

@property (nonatomic, copy) void (^onButtonClick)(NSString *title); //This is block

然后在按钮的目标方法中自定义 UITableViewCell 实现(.m 文件)(如果您仅在 UIAction 方法中使用 IB):

- (IBAction)buttonAction:(id)sender {
UIButton *button = (UIButton*)sender;
if (self.onButtonClick) { //Check if block set
self.onButtonClick(button.titleLabel.text); //Call block
}
}

然后在你的 cellForRowAtIndexPath:(NSIndexPath)indexPath 方法中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//...cell configurations
//...

cell.onButtonClick = ^void(NSString *title) {
//do what you want with your button's title
};
}

关于ios - 获取位于 UITableView 中的 UIButton 的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27035103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com