我有查看单元格的tableview,每个单元格都会推送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打印单元格的名称
.h of detailViewController
@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
。
答案 0 :(得分: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
标题中有DetailViewController
aString
.h of detailViewController
@property (nonatomic, strong) NSString *aString;
.m of detailViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.detailViewName.text = self.aString;
NSLog(@"%@",self.aString);
}