prepareForSegue not setting UILabel

会有一股神秘感。 提交于 2019-12-13 18:25:30

问题


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;

    }
}

My property declared in the header of my destinationVC:

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

Solution from answers below:

Mystery solved! Here's what I did:

on my destinationVC, I added this to my header:

@property (nonatomic, strong) NSString *destinationName;

I put this back in the implementation:

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

In destinationVC, I added this to my viewDidLoad:

self.destinationLabel.text = self.destinationName;

回答1:


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.

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.




回答2:


IBOutlet objects are not initialized until the view controller's view loads. That happens after the segue. Create a custom property and update that during prepare and then copy it to your label during viewDidLoad.



来源:https://stackoverflow.com/questions/29239648/prepareforsegue-not-setting-uilabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!