简体   繁体   English

在视图控制器之间传递数据不起作用

[英]Passing Data Between View Controllers Not Working

I am attempting to pass the label for a given selected cell to a different view controller... 我正在尝试将给定选定单元格的标签传递给其他视图控制器...

ListViewController.m ListViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:selectedIndexPath];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle: nil];
DetailViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"DetailVC"];
viewController.venueName = cell.textLabel;
[self.navigationController pushViewController:viewController animated:YES];

}

Here is my DetailViewController: 这是我的DetailViewController:

.h 。H

@interface DetailViewController : UIViewController {

    IBOutlet UILabel *venueName;
}

@property (nonatomic, strong) IBOutlet UILabel *venueName;

venueName never gets the data. 场所名称从不获取数据。 In my .m I also synthesize the venueName. 在我的.m文件中,我还综合了场所名称。

This happens because your UILabel outlet is not connected at the time you set a string for it. 发生这种情况是因为在为它设置字符串时未连接UILabel插座。 I recommend you to create a string property to store the value and, in viewDidLoad in DetailViewController, you set the string to venueName. 我建议您创建一个字符串属性来存储值,并在DetailViewController的viewDidLoad中将字符串设置为会场名称。 Cheers!! 干杯!!

您需要像设置和获取文本一样修改以下内容:-

viewController.venueName.text = cell.textLabel.text;

So venueName , which is a pointer to a label , is made to point to an existing label that's part of a table cell. 因此, venueName名称( 指向标签指针)被指向一个现有的标签,该标签是表单元格的一部分。 This is obviously wrong -- you don't mean to use the actual label from the table cell, but rather the text stored in it. 这显然是错误的-您并不是要使用表格单元格中的实际标签,而是要使用其中存储的文本。 Both the other answers here are correct: you surely mean to assign the text property of one label to that of the other, and the label that outlet viewController.venueName is connected to hasn't been created at the time this code executes. 这两个其他答案都是正确的:您肯定是要将一个标签的text属性分配给另一个标签,并且在执行此代码时尚未创建出口viewController.venueName连接到的标签。

The immediate thing you should do to fix your code is to assign the data in question (ie cell.textLabel.text ) to a string property in the destination view controller. 您应该立即修复代码的问题是将有问题的数据(即cell.textLabel.text )分配给目标视图控制器中的字符串属性。 Have the -viewDidLoad or -viewWillAppear method in that controller set the text property of the venueLabel label. 使该控制器中的-viewDidLoad-viewWillAppear方法设置venueLabel标签标签的text属性。 Either of those methods will be called after the view hierarchy is instantiated. 实例化视图层次结构后,将调用这些方法中的任何一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM