ios pass values during a segue to another view

2020-02-17 09:12发布

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);
}
}

5条回答
够拽才男人
2楼-- · 2020-02-17 09:38

You don't need to create a local instance variable of the destination view controller. This will do the same:

[segue.destinationViewController setLocTitle: [myValue valueForKeyPath:@"title"];
[segue.destinationViewController setLocInfo: [myValue valueForKeyPath:@"info"];
查看更多
The star\"
3楼-- · 2020-02-17 09:39

You can try this:

[[NSString alloc] initWithFormat:@"%@",
  [segue.destinationViewController setLocTitle: [myValue valueForKeyPath:@"title"]];
[[NSString alloc] initWithFormat:@"%@",
  [segue.destinationViewController setLocTitle: [myValue valueForKeyPath:@"info"]];
查看更多
神经病院院长
4楼-- · 2020-02-17 09:40

Another option - could use the following method:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

which fires before prepareForSegue.

查看更多
三岁会撩人
5楼-- · 2020-02-17 09:44

I think I had absolutely the same problem, so hope my solution will help you too. It should answer first part of your question, or at least will help some one else.

First of all you can't assign passed values to properties with viewDidLoad method in the SecondViewController. Doing so, gives nil, as viewDidLoad is implemented before Segue.

And I wouldn't recommend to use viewDidAppear, as as the values will be changed after the view is loaded, witch makes the change process be visually visible.

So, the best option is to assign the values directly to the IBOutlet's. If you want to pass an array of strings, you can assign one array value witch you want to load first, and also pass all NSArray. This will allow your view to be loaded with the value already, and also you will have other values to work with.

You could also call method and pass data.

查看更多
唯我独甜
6楼-- · 2020-02-17 10:00

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
    }
}
查看更多
登录 后发表回答