简体   繁体   English

将数据从tableView传递到另一个tableView

[英]Passing data from tableView to another tableView

I want to send data from SportVC to CCMatchForSportTableViewController 我想将数据从SportVC发送到CCMatchForSportTableViewController

yes I have imported the destination class in the header file in SportVC.h 是的,我已经在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 这是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): 但是当我在CCMatchForSportTableViewController.h的viewDidLoad方法和单元格标题中这样做时(当然是另一种方法):

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

it says 它说

kontolasd (null)

When you call performSegueWithIdentifier it's going to load your view controller from your storyboard. 当您调用performSegueWithIdentifier它将从情节performSegueWithIdentifier中加载视图控制器。 The one you created and used for matchForSport.sportSelected is going to be released when the method ends. 方法结束时,将释放您为matchForSport.sportSelected创建并使用的matchForSport.sportSelected You need to implement a prepareForSegue method and make your assignment there, using the destination controller. 您需要使用目标控制器实现prepareForSegue方法并在那里进行分配。

When the view loads of course no rows are selected, you need to move that line inside of didselect: 当视图加载当然没有选择任何行时,您需要将该行移到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. 这将显示您选择的值,然后在performSegue委托方法中,您可以将值传递给下一个控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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