ios pass values during a segue to another view

后端 未结 5 739
暗喜
暗喜 2020-12-24 12:15

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

相关标签:
5条回答
  • Another option - could use the following method:

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

    which fires before prepareForSegue.

    0 讨论(0)
  • 2020-12-24 12:57

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-24 13:10

    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.

    0 讨论(0)
  • 2020-12-24 13:15

    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"];
    
    0 讨论(0)
  • 2020-12-24 13:22

    You can try this:

    [[NSString alloc] initWithFormat:@"%@",
      [segue.destinationViewController setLocTitle: [myValue valueForKeyPath:@"title"]];
    [[NSString alloc] initWithFormat:@"%@",
      [segue.destinationViewController setLocTitle: [myValue valueForKeyPath:@"info"]];
    
    0 讨论(0)
提交回复
热议问题