使用故事板在两个控制器之间传递数据 [英] Passing data between two controllers using storyboards
问题描述
我正在尝试将数据从一个 UITableViewController
传递给另一个。这是我在初始视图控制器中的代码:
I am trying to pass data from one UITableViewController
to another. This is my code in the initial view controller:
- (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并在中记录
显示为null。主题
时,ListsViewController
The log output was showing that it had the data I wanted had been passed over. However when I perform the segue and log the subject
in the ListsViewController
is shows null.
任何想法?
推荐答案
您需要覆盖 prepareForSegue:sender:
方法。快速解决方案是
You need to overwrite prepareForSegue:sender:
method. The quick fix would be
- (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
实例,并为它分配了一个主题
,但这个视图控制器从未出现过。相反, performSegueWithIdentifier:sender
创建 ListsViewController
类的另一个实例,该类对主题<一无所知/ code>。这就是为什么你需要等待UIStoryboardSegue从故事板中实例化目标视图控制器,然后按照你想要的方式配置它,你可以在
prepareForSegue:sender:
方法中进行配置。
The reason why your code did not work is that in your showList:animated:
method you created a ListsViewController
instance and assigned it a subject
, but this view controller was never presented. Instead performSegueWithIdentifier:sender
creates another instance of your ListsViewController
class which knows nothing about your subject
. That's why you need to wait for UIStoryboardSegue to instantiate a destination view controller from the storyboard and then configure it the way you want, which you can do in prepareForSegue:sender:
method.
在 performSegueWithIdentifier中使用
方法,因为它不是发送者:)。我要做的是在视图控制器类中创建一个属性主题并使用它 subject
作为发件人可能不是最佳选择:sender prepareForSegue:sender:
Also it might be not the best idea to use subject
as a sender in performSegueWithIdentifier:sender
method, because it's just not the sender :). What I would do is create a property subject in your view controller class and use it 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
这篇关于使用故事板在两个控制器之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!