Ios 当按钮放置在UITableViewCell中时,如何从按钮单击传递数据
我有一个表视图,其中每一行都有一个按钮。我将数据存储为按钮的标题 我需要什么:我想将所选按钮的标题传递给目标视图控制器 代码:Ios 当按钮放置在UITableViewCell中时,如何从按钮单击传递数据,ios,objective-c,uitableview,segue,Ios,Objective C,Uitableview,Segue,我有一个表视图,其中每一行都有一个按钮。我将数据存储为按钮的标题 我需要什么:我想将所选按钮的标题传递给目标视图控制器 代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSDictionary *dict = _json[indexPath.row] [cell.bRedeem setTitle:dict[@"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *dict = _json[indexPath.row]
[cell.bRedeem setTitle:dict[@"n_id"] forState:UIControlStateNormal];
}
- (IBAction)redeemClicked:(UIButton *)sender {
UIButton *button = (UIButton *)sender;
_nID = button.currentTitle;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"showOffers"]) {
offerList *details = (offerList *)segue.destinationViewController;
details.nearID = _nID;
}
}
可以在cellForRowAtIndexPath方法中添加按钮目标
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell.btnOne addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.btnOne setTag:indexPath.row];
}
在按钮操作中,您可以使用[sender tag]值获取Json数组的值
-(IBAction)ButtonClicked:(id)sender
{
NSDictionary *dict = [JsonArray objectAtIndex:[sender tag]];
NSLog(@"Selected Dictionary : %@",dict);
NSString * storyboardName = @"Main"; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
NewViewController * NewView = [storyboard instantiateViewControllerWithIdentifier:@"NewViewControllerID"];
NewView.NearId = [dict valueForKey@"n_id"];
[self.navigationController pushViewController:NewView animated:YES];
}
实施TableView单元格委托。您在哪里执行performSegue?有关一系列解决方案,请参阅。一般来说,你不应该依赖单元格的内容,仅仅是它的索引路径,然后你就可以使用你的数据源。获取信息很容易,但是现在我想传递数据。你能告诉我传递数据的方法吗?我正在传递类似//NSString*storyboardName=@“Main”这样的数据;UIStoryboard*情节提要=[UIStoryboard情节提要WithName:storyboard名称bundle:nil];NewViewController*NewView=[storyboard InstanceEviewController标识符:@“NewViewControllerID”];NewView.passedData=@“您的数据”;[self.navigationController pushViewController:NewView动画:是];您需要在destinationViewController.h中声明一个要导航的变量。比如,//@property(非原子,强)NSString*PassedData;我也这么做了,但是更新答案返回一个值nil你在按钮操作中得到了按钮标题吗?