prepareForSegue没有设置UILabel

我有一个prepareForSegue方法安装程序,发送一切我想要的destinationViewController除了destinationViewController UILabel上的UILabel的值。 我扔了一个NSLog语句,看看它打印什么值,它打印我想要的。

它不会崩溃,但它不会设置。 我知道我在这里错过了一些非常基本的东西,但是它并没有跳出我的身边。

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton *)sender { if ([[segue identifier] isEqualToString:@"directionsSegue"]) { // Set destination view controller DirectionsViewController *destinationVC = segue.destinationViewController; // Pick out the "thing" you want to send to destinationVC CGPoint point = [sender convertPoint:CGPointZero toView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point]; // Set the "thing" on the destinationVC PointOfInterest *poi = [self.fetchedResultsController objectAtIndexPath:indexPath]; destinationVC.destinationLabel.text = poi.name; NSLog(@"destinationVC.destinationLabel.text = %@", poi.name); destinationVC.destinationLatitude = poi.latitude; destinationVC.destinationLongitude = poi.longitude; } } 

我的属性声明在我的destinationVC头:

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

从下面的答案解决:

谜团已揭开! 以下是我所做的:

在我的目标VC上,我把这个添加到我的头文件中:

 @property (nonatomic, strong) NSString *destinationName; 

我把这个放在执行中:

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

destinationVC ,我将其添加到我的viewDidLoad

 self.destinationLabel.text = self.destinationName; 

prepareForSegue你的标签将是零,因为它不会在这个时候实例化。 事实上,一旦你的视图被加载,IBOutlet就会被初始化。 这就是为什么它不工作。

解决您的问题的最佳方法是在DirectionsViewController中创build另一个属性,其中将存储您的文本。 这个可以在控制器初始化之后直接使用,然后你可以直接在你的控制器的任何地方设置你的标签。

在加载视图控制器的视图之前,IBOutlet对象不会被初始化。 那是在赛后发生的。 创build自定义属性并在准备过程中进行更新,然后在viewDidLoad期间将其复制到您的标签。