0

I have a table view in which each row is having a button. Am storing the data as the button's title.

What I need : I want to pass the selected button's title to the destination view controller.

Code :

- (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;

    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
TheTravloper
  • 236
  • 5
  • 16
  • where are you doing performSegue? – Bhavin Kansagara Aug 20 '18 at 10:59
  • See [here](https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510#38941510) for a range of solutions. In general, you shouldn't rely on the content of the cell, merely its index path which you can then use with your data source – Paulw11 Aug 20 '18 at 12:12

2 Answers2

0

You can add a Button target in your cellForRowAtIndexPath method.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    [cell.btnOne addTarget:self action:@selector(ButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell.btnOne setTag:indexPath.row];

}

In the button action you can get the value of your Json Array using the [sender tag] value.

-(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];
}
Vinu Jacob
  • 393
  • 2
  • 15
  • 1
    well getting the information is easy but now i want to pass the data. Can you please tell me the method to pass the data – TheTravloper Aug 20 '18 at 11:06
  • 1
    I am passing data like //NSString * storyboardName = @"Main"; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil]; NewViewController * NewView = [storyboard instantiateViewControllerWithIdentifier:@"NewViewControllerID"]; NewView.passedData = @"Your data"; [self.navigationController pushViewController:NewView animated:YES]; – Vinu Jacob Aug 20 '18 at 11:09
  • You need to declare a variable in your destinationViewController.h that you want to navigate. Like, // @property(nonatomic,strong) NSString *PassedData; – Vinu Jacob Aug 20 '18 at 11:15
  • i did the same but the update answer returns a value nil – TheTravloper Aug 20 '18 at 11:23
  • do you get the Button title in the button action? – Vinu Jacob Aug 20 '18 at 11:38
  • NIL?? from which line you are getting nil? – Vinu Jacob Aug 20 '18 at 12:12
0

Implement TableView cell delegates.

GeneCode
  • 7,545
  • 8
  • 50
  • 85