0

我有带有单元格的表格视图,每个单元格都会推送 detailViewController。我想将单元格的文本分配给 detailViewController 中的 textLabel。

.h 类 masterView

@property (strong,nonatomic) NSString *selectedCellText;

.m 类 masterView

@synthesize selectedCellText = _selectedCellText;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedCellText = [[[tableView cellForRowAtIndexPath:indexPath] textLabel] text];
    NSLog(@"%@",self.selectedCellText);
}

这里 NSLog 打印单元格的名称

detailViewController 的.h

@property (nonatomic, copy) listOfTricksViewController *aReference;

.m of detailViewController

@synthesize aReference = _aReference;

- (void)viewDidLoad
{

    [super viewDidLoad];
    self.detailViewName.text = self.aReference.selectedCellText;
     NSLog(@"%@",self.aReference.selectedCellText);
} 

这里 NSLog 打印null

4

1 回答 1

0

像这样修改你的方法prepareForSegue

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{ 
    if([[segue identifier] isEqualToString:@"detailTrick"])
    { 
        DetailViewController *detailViewController = (DetailViewController *)segue.
        NSString *object = nil; 
        NSIndexPath *indexPath = nil;
        indexPath = [self.tableView indexPathForSelectedRow]; 

        // Pass data
        detailViewController.aString = self.cellObjects[indexPath.row]; 
    } 
} 

您需要调用您DetailViewController的数据并将数据转发到此控制器(假设@property您的标头中有一个 aString DetailViewController

detailViewController 的.h

@property (nonatomic, strong) NSString *aString;

.m of detailViewController

- (void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];

    self.detailViewName.text = self.aString;
     NSLog(@"%@",self.aString);
} 
于 2013-10-06T11:46:02.007 回答