1

我正在尝试将数据从一个传递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];
}

日志输出显示它已经传递了我想要的数据。但是,当我执行 segue 并登录subject时,ListsViewController显示为 null。

有任何想法吗?

4

4 回答 4

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
于 2013-10-18T08:52:00.980 回答
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;
}
于 2013-10-18T08:58:16.107 回答
0

在此方法中实施 prepareForSegue 并通过日期

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([seque.identifier isEqualToString:@"showDetail"])
{
ListsViewController *lists = seque.destinationViewController;
lists.subject = subject;
}
}
于 2013-10-18T08:53:08.027 回答
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;
}
于 2013-10-18T08:55:26.353 回答