177

in my app I've a button that performs a segue programmatically:

- (void)myButtonMethod
{
    //execute segue programmatically
    [self performSegueWithIdentifier: @"MySegue" sender: self];
}

I would like to know if there is a way to reference the destination view and to pass it some parameters.

I know that in prepareForSegue method, I can refer to it with:myDestinationViewController *vc = [segue destinationViewController];, but I don't know how to this executing the segue programmatically.

Do you have any ideas?

Thanks, yassa


UPDATE:

I'm sorry for this question!!! I simply discovered that, even if the segue is invoked programmatically, the prepareForSegue method is called anyway and so it is possible to pass parameters in the same usual way.

yassassin
  • 3,185
  • 6
  • 28
  • 31

5 Answers5

108

The answer is simply that it makes no difference how the segue is triggered.

The prepareForSegue:sender: method is called in any case and this is where you pass your parameters across.

trapper
  • 11,716
  • 7
  • 38
  • 82
  • 4
    This still bothers me - this means I have to trigger segue in one place, where I have the data that I want to send as parameters nicely isolated from anything else, but to add that data in a completely different place - the prepareForSegue, where we don't know anything about the data needed to be passed. Meaning, I have to create a state with that data in question in the context shared by two different functions (one triggering segue, the other - preparing). I don't want to dirty the state when all I need is to open a new view with some data... – NeverwinterMoon Sep 27 '18 at 12:39
  • Use sender to pass context needed to configure the destination VC – trapper Sep 27 '18 at 12:42
  • I assumed that passing payload in sender is an anti-pattern. It should just contain the info about who triggered the segue, not the data that the thing that triggered the segue intended to pass further. – NeverwinterMoon Sep 27 '18 at 12:51
  • From the official docs on sender: "This object is made available for informational purposes during the actual segue." – NeverwinterMoon Sep 27 '18 at 12:51
  • 1
    Yeah don’t pass an actual payload, pass context, like which cell/button/index/etc triggered the segue. – trapper Sep 27 '18 at 12:54
91

Old question but here's the code on how to do what you are asking. In this case I am passing data from a selected cell in a table view to another view controller.

in the .h file of the trget view:

@property(weak, nonatomic)  NSObject* dataModel;

in the .m file:

@synthesize dataModel;

dataModel can be string, int, or like in this case it's a model that contains many items

- (void)someMethod {
     [self performSegueWithIdentifier:@"loginMainSegue" sender:self];
 }

OR...

- (void)someMethod {
    UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeController"];
    [self.navigationController pushViewController: myController animated:YES];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"storyDetailsSegway"]) {
        UITableViewCell *cell = (UITableViewCell *) sender;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        NSDictionary *storiesDict =[topStories objectAtIndex:[indexPath row]];
        StoryModel *storyModel = [[StoryModel alloc] init];
        storyModel = storiesDict;
        StoryDetails *controller = (StoryDetails *)segue.destinationViewController;
        controller.dataModel= storyModel;
    }
}
Salman Zaidi
  • 9,342
  • 12
  • 44
  • 61
user1723341
  • 1,107
  • 8
  • 7
3

Swift 4:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ExampleSegueIdentifier" {
        if let destinationVC = segue.destination as? ExampleSegueVC {
            destinationVC.exampleString = "Example"
        }
    }
}

Swift 3:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "ExampleSegueIdentifier" {
            if let destinationVC = segue.destinationViewController as? ExampleSegueVC {
                destinationVC.exampleString = "Example"
            }
        }
    }
Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45
0

I understand the problem of performing the segue at one place and maintaining the state to send parameters in prepare for segue.

I figured out a way to do this. I've added a property called userInfoDict to ViewControllers using a category. and I've override perform segue with identifier too, in such a way that If the sender is self(means the controller itself). It will pass this userInfoDict to the next ViewController.

Here instead of passing the whole UserInfoDict you can also pass the specific params, as sender and override accordingly.

1 thing you need to keep in mind. don't forget to call super method in ur performSegue method.

0

In case if you use new swift version.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "ChannelMoreSegue" {

        }
}
Pokotuz
  • 121
  • 1
  • 5