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

发布于 2025-01-03 14:21:11 字数 2751 浏览 4 评论 0原文

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

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)

我已经研究了一段时间了,想弄清楚为什么它是(空)。有谁知道为什么会发生这种情况?我错过了什么吗?

感谢您抽出时间!

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

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>

有没有办法让我的应用程序加载?

In my IB I created a segue to push from one UIViewController (identifier = List) to another (identifier = Details). Then in

prepareForSegue

I do this to carry over some data for testing purposes:

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");

Yet almost all the NSLogs return (null) values from the same detailsViewController the data was sent to.

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)

I've been at it for a while now, to figure out why it's (null). Does anyone have any clue why this happens? Am I missing something?

Thanks for your time!

EDIT: The Detail View thats edited and the one thats loaded seem defferent, but that does not explain why the logs return (null) coz the fetch data from the same Details view as the one thats edited.

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>

Is there a way I can tell my app to load the instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赤濁 2025-01-10 14:21:11

因为在调用 -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;
}

Because the outlets are not getting initialized before -viewDidLoad method gets called. Create iVars for each value you want to store and assign them after the init method of destination controller. Then assign those values to outlets in viewDidLoad.

e.g.

in your destination controller's .h file

@property (nonatomic, copy) NSString *noteText;

Then

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

And in detailsViewController -viewDidLoad method

- (void)viewDidLoad {
  [super viewDidLoad];
  self.NotesArea.text = noteText;
}
小巷里的女流氓 2025-01-10 14:21:11

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

If you read properly in Apple documentation for this method, they mention at the end that
" This method creates a new instance of the specified view controller each time you call it."
So you got to be very careful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文