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

时间:2021-08-08 22:59:21

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

这是将数据发送到第二个视图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)

2 个解决方案

#1


0  

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.

当你调用performSegueWithIdentifier时,它将从你的故事板中加载你的视图控制器。您为matchForSport.sportSelected创建和使用的那个将在方法结束时释放。您需要使用目标控制器实现prepareForSegue方法并在那里进行分配。

#2


0  

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委托方法中,您可以将值传递给下一个控制器。

#1


0  

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.

当你调用performSegueWithIdentifier时,它将从你的故事板中加载你的视图控制器。您为matchForSport.sportSelected创建和使用的那个将在方法结束时释放。您需要使用目标控制器实现prepareForSegue方法并在那里进行分配。

#2


0  

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委托方法中,您可以将值传递给下一个控制器。