0

在我的 IB 中,我创建了一个从一个 UIViewController(标识符 = 列表)推送到另一个(标识符 = 详细信息)的 segue。然后在

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 返回(空)值。

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)。有谁知道为什么会这样?我错过了什么吗?

谢谢你的时间!

编辑:已编辑的详细信息视图和已加载的详细信息视图似乎不同,但这并不能解释为什么日志返回(空),因为从与已编辑的详细信息视图相同的详细信息视图中获取数据。

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>

有没有办法告诉我的应用程序加载呢?

4

2 回答 2

0

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

例如

在目标控制器的 .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;
}
于 2012-02-08T12:07:22.183 回答
0

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

于 2012-02-08T12:14:49.120 回答