title: ios pass values during a segue to another view
body: <p>Trying to do something I've seen documented in a lot of places, but can't seem to manage. I'm trying to pass data from one view to another during a segue.</p>
  
  <p>Here's the prepareForSegue method in the source view:</p>
  
  <pre><code>- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  {
  
      //pass values
  DetailViewController *dest = (DetailViewController *)[segue destinationViewController];
  
  dest.locTitle = [value valueForKeyPath:@"title"];
  dest.locInfo = [value valueForKeyPath:@"info"];
  
  
  
  // NSLog(@"The title is: %@, and the info is: %@.",dest.locTitle,dest.locInfo);
  
  }
  </code></pre>
  
  <p>If I enable that last <code>NSLog</code>, I see the correct values...</p>
  
  <p>Here's the interface on the destination view:</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  @interface DetailViewController : UIViewController{
  
  }
  
  @property (strong, nonatomic) NSString *locTitle;
  @property (strong, nonatomic) NSString *locInfo;
  @property (weak, nonatomic) IBOutlet UILabel *detailMainText;
  @property (weak, nonatomic) IBOutlet UILabel *detailTitle;
  
  @end
  </code></pre>
  
  <p>... and here's the viewDidLoad and relevant @synthesize on the destination view:</p>
  
  <pre><code>@synthesize detailMainText,detailTitle,locTitle,locInfo;
  
  - (void)viewDidLoad
  {
  [super viewDidLoad];
  NSLog(@"In viewDidLoad - the title is: %@ and the info is: %@",self.locTitle,self.locInfo);
  detailTitle.text = locTitle;
  detailMainText.text = locInfo;
  }
  </code></pre>
  
  <p>That <code>NSLog</code> reports <code>null</code> values for each of those variables.</p>
  
  <p>I'd appreciate any help you could give me. I'm sure I'm doing something stupid, and obvious. </p>
  
  <p>Thanks in advance for your help.</p>
  
  <hr>
  
  <h2>Much later...</h2>
  
  <hr>
  
  <p>OK... I think I'm narrowing it down. </p>
  
  <p>I've discovered that (counterintuitively)</p>
  
  <pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  </code></pre>
  
  <p>gets called <strong>after</strong></p>
  
  <pre><code>- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  </code></pre>
  
  <p>Which is confusing, and also hosed my little scheme to figure out what chunk of my <code>NSDictionary</code> datasource should be displayed in my detail view. Currently I'm doing this:</p>
  
  <pre><code>// send info to detail view according to which row selected
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  
      //Get the selected object in order to fill out the detail view
      myValue = [myLocations objectForKey:[locationsIndex objectAtIndex:indexPath.row]];
  
  }
  </code></pre>
  
  <p>But since this happens <strong>after</strong> the segue, it's kinda useless. How can I access the row number in the prepareForSegue method?</p>
  
  <p>Thanks again.</p>
  
  <hr>
  
  <h2>... and finally...</h2>
  
  <hr>
  
  <p>Here's the answer to the question: </p>
  
  <p><strong>How do I access a selected row number while in the prepareForSegue method?</strong> </p>
  
  <p>Hopefully someone will find it useful.</p>
  
  <p>First, set up a <code>IBOutlet</code> for the tableview in the listView controller's interface:</p>
  
  <pre><code>@property (weak, nonatomic) IBOutlet UITableView *tableView;
  </code></pre>
  
  <p>Then your prepareForSegue can do this:</p>
  
  <pre><code>- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  {
  if ([[segue identifier] isEqualToString: @"showDetail"]) {
      //pass values
      NSLog(@"The sender is %@",sender);
  
      NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
      //Get the selected object in order to fill out the detail view
      id myValue = [myLocations objectForKey:[locationsIndex objectAtIndex:indexPath.row]];
  
      DetailViewController *dest = [segue destinationViewController];
      dest.locTitle = [myValue valueForKeyPath:@"title"];
      dest.locInfo = [myValue valueForKeyPath:@"info"];
  
      //NSLog(@"The title is: %@, and the info is: %@.",dest.locTitle,dest.locInfo);
  }
  }
  </code></pre>