54602

prepareForSegue not setting UILabel

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.

Answer2:

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.

Recommend

  • UIBezierPath not updated as expected
  • Opengl Iphone SDK: How to tell if you're touching an object on screen?
  • SKPhysicsJointFixed doesn't keep nodes together while moving one
  • Get touch points in UIScrollView through UITapGestureRecognizer
  • animating a custom CALayer property
  • Drag and drop without removing UIButton
  • How do I know what reference to include to import a specific .NET namespace?
  • SSRS 2008 - Sorting within a group
  • Peer to peer/adaptive payments with paypal [closed]
  • UrlEncode non-string properties for HTTP Post through HttpClient
  • FTPWebRequest .NET 3.5 vs 4
  • Python Paramiko send CTRL+C to an ssh shell
  • Send the string to its hex equivalent
  • JSON data through JS/AJAX into PHP
  • How to best manage SMTP clients
  • PHP get selected value of select form
  • Powershell Hash Table to HTML
  • Can you pass an array from javascript to asp.net mvc controller action without using a form?
  • Compress a file with GZipStream while maintaining its meta-data
  • What's the syntax to inherit documentation from another indexer?
  • Cannot get text from text area
  • Why the SequenceFile is truncated?
  • Sending keystrokes/mouse clicks to a Java program with Autohotkey
  • DIV instruction jumping to random location?
  • Why cepheus don't send int without quotes to orion?
  • ASP.NET MVC Application won't update some controllers
  • CakePHP ACL tutorial initDB function warnings
  • iOS: Detect app start via notification press
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Email verification using google app script and google forms
  • Get data from AJAX - How to
  • Using variable in a value field in jMeter
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Updating server-side rendering client-side
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • How to pass list parameters for each object using Spring MVC?
  • bootstrap to use multiple ng-app
  • sending mail using smtp is too slow
  • Binding checkboxes to object values in AngularJs
  • How to load view controller without button in storyboard?