Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios pass values during a segue to another view

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.


Much later...


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.


... and finally...


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); } } 
like image 875
james Burns Avatar asked Mar 28 '12 12:03

james Burns


People also ask

How do you pass data through unwind segue?

You create the unwind segue as per usual - drag to the exit icon in the scene. Now in the object inspector on the left you will see the unwind segue listed below the view controller, first responder and exit icons. You can click on the unwind segue and give it an identifier in the inspector on the right.

What is unwind segue iOS?

To handle the dismissal of a view controller, create an unwind segue. Unlike the segues that you use to present view controllers, an unwind segue promises the dismissal of the current view controller without promising a specific target for the resulting transition.


1 Answers

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     } } 
like image 195
ltebean Avatar answered Oct 22 '22 21:10

ltebean