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.
Here's the prepareForSegue method in the source view:
- (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);
}
If I enable that last NSLog
, I see the correct values...
Here's the interface on the destination view:
#import <UIKit/UIKit.h>
@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
... and here's the viewDidLoad and relevant @synthesize on the destination view:
@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;
}
That NSLog
reports null
values for each of those variables.
I'd appreciate any help you could give me. I'm sure I'm doing something stupid, and obvious.
Thanks in advance for your help.
OK... I think I'm narrowing it down.
I've discovered that (counterintuitively)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
gets called after
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
Which is confusing, and also hosed my little scheme to figure out what chunk of my NSDictionary
datasource should be displayed in my detail view. Currently I'm doing this:
// 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]];
}
But since this happens after the segue, it's kinda useless. How can I access the row number in the prepareForSegue method?
Thanks again.
Here's the answer to the question:
How do I access a selected row number while in the prepareForSegue method?
Hopefully someone will find it useful.
First, set up a IBOutlet
for the tableview in the listView controller's interface:
@property (weak, nonatomic) IBOutlet UITableView *tableView;
Then your prepareForSegue can do this:
- (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);
}
}
you can achieve it by doing this: first pass the value you want to send to the destination controller here:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@"segueToLocation" sender:the object you want to pass];
}
then get the object here
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString: @"segueToLocation"]) {
DetailViewController *dest = (DetailViewController *)[segue destinationViewController];
//the sender is what you pass into the previous method
dest.target=sender
}
}