简体   繁体   中英

Passing data from tableView to another tableView

I want to send data from SportVC to CCMatchForSportTableViewController

yes I have imported the destination class in the header file in SportVC.h

#import "CCMatchForSportTableViewController.h"

this is the method to send data to second view Controller

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath      *)indexPath
{
    CCMatchForSportTableViewController *matchForSport =     [[CCMatchForSportTableViewController alloc] init];
    matchForSport.sportSelected = [self.sport objectAtIndex:indexPath.row];
    [self performSegueWithIdentifier:@"SportToMatchSegue" sender:self];
}

this is property in the header file in CCMatchForSportTableViewController.h

@interface CCMatchForSportTableViewController : UITableViewController

@property (strong,nonatomic) NSString *sportSelected;

@end

but when I do this in the viewDidLoad method in CCMatchForSportTableViewController.h and in the cell title(another method of course):

NSLog(@"kontolasd %@", self.sportSelected);

it says

kontolasd (null)

When you call performSegueWithIdentifier it's going to load your view controller from your storyboard. The one you created and used for matchForSport.sportSelected is going to be released when the method ends. You need to implement a prepareForSegue method and make your assignment there, using the destination controller.

When the view loads of course no rows are selected, you need to move that line inside of didselect:

- (void)tableView:(UITableView *)tableView 
                       didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
    CCMatchForSportTableViewController *matchForSport =    
                                    [[CCMatchForSportTableViewController alloc] init];
    matchForSport.sportSelected = [self.sport objectAtIndex:indexPath.row];
    self.sportSelected = matchForSport.sportSelected;
    NSLog(@"kontolasd %@", self.sportSelected);
    [self performSegueWithIdentifier:@"SportToMatchSegue" sender:self];
}

this will show you selected values, then in performSegue delegate method you can pass value to the next controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM