Perform Segue programmatically and pass parameters to the destination view
Solution 1
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.
Solution 2
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;
}
}
Solution 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"
}
}
}
Related videos on Youtube
yassassin
Updated on July 08, 2022Comments
-
yassassin almost 2 years
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.-
danielbeard about 12 yearsAdd the update as an answer and accept it so that people know this has been answered :)
-
DanSkeel about 12 yearsAdd answer, comment for notification.
-
lindon fox almost 12 yearsYou should mark the answer below as correct, so people know that it has been answered.
-
-
NeverwinterMoon almost 6 yearsThis 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...
-
trapper almost 6 yearsUse sender to pass context needed to configure the destination VC
-
NeverwinterMoon almost 6 yearsI 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 almost 6 yearsFrom the official docs on sender: "This object is made available for informational purposes during the actual segue."
-
trapper almost 6 yearsYeah don’t pass an actual payload, pass context, like which cell/button/index/etc triggered the segue.