传递给UIViewController的数据返回(null)

时间:2012-02-08 11:56:23

标签: objective-c uiviewcontroller setter segue

在我的IB中,我创建了一个segue,用于从一个UIViewController(identifier = List)推送到另一个(identifier = Details)。然后在

prepareForSegue

我这样做是为了测试目的而继承一些数据:

Detail *detailsViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Details"];

NSLog(@"Current \"Details\" class in use: %@",detailsViewController);

NSLog(@"Prep Complete, Testing begins==============");
detailsViewController.TitleField.text = @"Random Text";
detailsViewController.DuedateField.text = [NSString stringWithFormat:@"%@",[NSDate date]];
detailsViewController.ReminderFieldOne.text = [NSString stringWithFormat:@"%@",[NSDate date]];
detailsViewController.ReminderFieldTwo.text = [NSString stringWithFormat:@"%@",[NSDate date]];
detailsViewController.NotesArea.text = @"Note 1: This doesnt seem to work.";

NSLog(@"Items from %@:",detailsViewController);
NSLog(@"Title Field = %@",detailsViewController.TitleField.text);
NSLog(@"Duedate field = %@",detailsViewController.DuedateField.text);
NSLog(@"Reminder field 1 = %@",detailsViewController.ReminderFieldOne.text);
NSLog(@"Reminder field 2 = %@",detailsViewController.ReminderFieldTwo.text);
NSLog(@"Notes = %@",detailsViewController.NotesArea.text);

NSLog(@"===============Testing Complete");

然而,几乎所有NSLog都从发送数据的相同detailsViewController返回(null)值。

2012-02-08 13:38:53.016 TodoApp[10132:fb03] Items from <Detail: 0x6d70120>:
2012-02-08 13:38:53.017 TodoApp[10132:fb03] Title Field = (null)
2012-02-08 13:38:53.017 TodoApp[10132:fb03] Duedate field = (null)
2012-02-08 13:38:53.018 TodoApp[10132:fb03] Reminder field 1 = (null)
2012-02-08 13:38:53.019 TodoApp[10132:fb03] Reminder field 2 = (null)
2012-02-08 13:38:53.020 TodoApp[10132:fb03] Notes = (null)

我现在已经有一段时间了,想弄清楚为什么(null)。有没有人知道为什么会这样?我错过了什么吗?

谢谢你的时间!

编辑:已编辑的详细视图和加载的详细视图看起来不同,但这并不能解释为什么日志返回(null)可以从同一个详细信息视图中获取与编辑过的数据相同的数据。

2012-02-08 14:41:11.937 TodoApp[10567:fb03] Current "Details" class in use: <Detail: 0x6a883a0>
2012-02-08 14:41:11.937 TodoApp[10567:fb03] Prep Complete, Testing begins==============
2012-02-08 14:41:11.939 TodoApp[10567:fb03] Items from <Detail: 0x6a883a0>:
2012-02-08 14:41:11.940 TodoApp[10567:fb03] Title Field = (null)
2012-02-08 14:41:11.940 TodoApp[10567:fb03] Duedate field = (null)
2012-02-08 14:41:11.941 TodoApp[10567:fb03] Reminder field 1 = (null)
2012-02-08 14:41:11.942 TodoApp[10567:fb03] Reminder field 2 = (null)
2012-02-08 14:41:11.942 TodoApp[10567:fb03] Notes = (null)
2012-02-08 14:41:11.943 TodoApp[10567:fb03] ===============Testing Complete
2012-02-08 14:41:11.953 TodoApp[10567:fb03] View Loaded: <Detail: 0x6d1feb0>

有没有办法可以告诉我的应用加载代替?

2 个答案:

答案 0 :(得分:0)

因为在调用-viewDidLoad方法之前没有初始化插座。为要存储的每个值创建iVar,并在目标控制器的init方法之后分配它们。然后将这些值分配给viewDidLoad中的出口。

e.g。

在目标控制器的.h文件中

@property (nonatomic, copy) NSString *noteText;

然后

Detail *detailsViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Details"];
detailsViewController.noteText = @"Note 1: This doesnt seem to work.";

并在detailsViewController -viewDidLoad方法

- (void)viewDidLoad {
  [super viewDidLoad];
  self.NotesArea.text = noteText;
}

答案 1 :(得分:0)

如果您在Apple documentation中正确阅读此方法,他们最后会提到  “每次调用它时,此方法都会创建指定视图控制器的新实例。” 所以你必须非常小心。