
Question:
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.
<strong>It doesn't crash, but it doesn't set.</strong> 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;
<strong>Solution from answers below:</strong>
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;
Answer1: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.
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
.