我正在尝试将数据从一个UITableViewController
传递到另一个- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Subject *subject = (Subject *)[self.fetchedResultsController objectAtIndexPath:indexPath];
[self showList:subject animated:YES];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)showList:(Subject *)subject animated:(BOOL)animated {
ListsViewController *lists = [[ListsViewController alloc] initWithStyle:UITableViewStyleGrouped];
lists.subject = subject;
NSLog(@"%@", lists.subject);
[self performSegueWithIdentifier:@"showDetail" sender:self];
}
。这是我在初始视图控制器中的代码:
subject
日志输出显示它已经传递了我想要的数据。但是,当我执行segue并记录时,ListsViewController
中的{{1}}显示为空。
有什么想法吗?
答案 0 :(得分:2)
您需要覆盖prepareForSegue:sender:
方法。快速解决方案是
- (void)showList:(Subject *)subject animated:(BOOL)animated
{
[self performSegueWithIdentifier:@"showDetail" sender:subject];
}
...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showDetail"]) {
ListsViewController *controller = ([segue.destinationViewController isKindOfClass:[ListsViewController class]]) ? segue.destinationViewController : nil;
controller.subject = ([sender isKindOfClass:[Subject class]]) ? subject : nil;
}
}
您的代码无效的原因是您在showList:animated:
方法中创建了ListsViewController
实例并为其分配了subject
,但此视图控制器从未显示过。相反,performSegueWithIdentifier:sender
会创建另一个ListsViewController
类的实例,该实例对您的subject
一无所知。这就是为什么你需要等待UIStoryboardSegue从故事板中实例化目标视图控制器,然后按照你想要的方式配置它,你可以在prepareForSegue:sender:
方法中进行配置。
在subject
方法中使用performSegueWithIdentifier:sender
作为发件人可能不是最佳选择,因为它不是发件人:)。我要做的是在视图控制器类中创建一个属性主题并使用它prepareForSegue:sender:
@interface MyViewController ()
@property (strong, nonatomic) Subject *subject;
@end
@implementation MyViewController
- (void)showList:(Subject *)subject animated:(BOOL)animated
{
self.subject = subject;
[self performSegueWithIdentifier:@"showDetail" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showDetail"]) {
ListsViewController *controller = ([segue.destinationViewController isKindOfClass:[ListsViewController class]]) ? segue.destinationViewController : nil;
controller.subject = self.subject;
}
}
...
@end
答案 1 :(得分:0)
实施prepareForSegue并在此方法中传递日期
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([seque.identifier isEqualToString:@"showDetail"])
{
ListsViewController *lists = seque.destinationViewController;
lists.subject = subject;
}
}
答案 2 :(得分:0)
这很好,但现在你需要添加这个:
首先代替:
[self performSegueWithIdentifier:@"showDetail" sender:self];
您需要发送对象:
[self performSegueWithIdentifier:@"showDetail" sender:subject];
在ListsViewController.h中添加一个属性:
@property (nonatomic, strong) Subject * subjectSegue;
现在在你的第一个视图控制器中:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showDetail"]) {
ListsViewController * lists = (ListsViewController *)[segue destinationViewController];
lists.subjectSegue = sender;
}
答案 3 :(得分:0)
您需要了解performSegueWithIdentifier:sender:
创建一个新的视图控制器实例。因此,您创建的ListsViewController
不会显示在屏幕上。
您需要覆盖`prepareForSegue:sender:
- (void)showList:(Subject *)subject animated:(BOOL)animated
{
[self performSegueWithIdentifier:@"showDetail" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showDetail"]) {
ListsViewController *controller = (ListsViewController *)segue.destinationViewController;
controller.subject = self.subject;
}