prepareForSegue 没有设置 UILabel [英] prepareForSegue not setting UILabel

查看:22
本文介绍了prepareForSegue 没有设置 UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 prepareForSegue 方法设置,它将我想要的所有内容发送到 destinationViewController,除了 上的 UILabel 值目标VC.我扔了一个 NSLog 语句,看看它打印了什么值,它打印了我想要的.

I have a prepareForSegue method setup in that sends everything I want to the destinationViewController except the value for a UILabel on the destinationVC. I threw a NSLog statement in to see what value it prints and it prints what I want.

它不会崩溃,但不会设置.我知道我在这里遗漏了一些非常基本的东西,但它并没有跳出来.

It doesn't crash, but it doesn't set. I know I'm missing something very basic here, but it's not jumping out at me.

- (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 的标头中声明的属性:

My property declared in the header of my destinationVC:

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

以下答案的解决方案:

谜团解开!这是我所做的:

Mystery solved! Here's what I did:

在我的 destinationVC 上,我将此添加到我的标题中:

on my destinationVC, I added this to my header:

@property (nonatomic, strong) NSString *destinationName;

我把它放回了实现中:

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

destinationVC 中,我将其添加到我的 viewDidLoad 中:

In destinationVC, I added this to my viewDidLoad:

self.destinationLabel.text = self.destinationName;

推荐答案

您的标签在 prepareForSegue 中将为 nil,因为此时不会实例化.事实上,一旦你的视图被加载,IBOutlet 就会被初始化.这就是它不起作用的原因.

Your label will be nil in prepareForSegue because it won't be instantiate at this time. In fact, IBOutlet are initialised yet once your view is loaded. That's why it's not working.

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

The best way to solve your issue is to create another property in your DirectionsViewController where will be stored your text. This one is available directly after your controller initialisation, and then you can set your label directly wherever in your controller.

这篇关于prepareForSegue 没有设置 UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆