Reputation: 824
I have this view which on it there are three UIButtons that each of which has segue identifier pushing to one VC.
Here's my code for preparing the segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Button 1"]) {
[[segue destinationViewController] setBudgetOrderingViewController:self];
} else if ([segue.identifier isEqualToString:@"Button 2"]) {
[[segue destinationViewController] setBudgetOrderingViewController:self];
} else if ([segue.identifier isEqualToString:@"Button 3"]) {
[[segue destinationViewController] setBudgetOrderingViewController:self];
}
}
Is there a way to know what segue identifier which load the destinationVC on the destinationVC?
Thanks.
Upvotes: 1
Views: 710
Reputation: 733
//Declare a string Property in Destination View Controller
@property (strong, nonatomic) NSString *Segue_Listner;
//In Source ViewController perform segue method
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier]isEqualToString:@"destinationVC"]) {
DestinationVC *dvc = [segue destinationViewController];
dvc.Segue_Listner = @"somevalue";
}
Passing whole view controller is ok, But we have to be careful if the view controller object size is too big. Also make sure to declare destinationVC property as weak(ONLY IF YOU ARE PASSING VIEWCONTROLLER AND IT IS STILL IN THE MEMORY),i.e presenting modally.
I recommend its safe to declare a string property and made it set by previous/SourceVC.
Upvotes: 1
Reputation: 385890
You can give the destination view controller a property that identifies the segue, and set that property in the source view controller's prepareForSegue:sender:
method. Example:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
DestinationViewController *dvc = segue.destinationViewController;
dvc.segueIdentifier = segue.identifier;
if ([segue.identifier isEqualToString:@"Button 1"]) {
[dvc setBudgetOrderingViewController:self];
} else if ([segue.identifier isEqualToString:@"Button 2"]) {
[dvc setBudgetOrderingViewController:self];
} else if ([segue.identifier isEqualToString:@"Button 3"]) {
[dvc setBudgetOrderingViewController:self];
}
}
However, this is actually not a very good design. Now you have two view controllers that need to know all of the segue identifiers. You might forget to update one of them if you change an identifier or add a new one.
A better design is to make the source view controller tell the destination view controller what to do.
Let's use a concrete example. Suppose your app lets the user send a card to a friend when the friend has a baby. So your main screen has three buttons: “It's a boy!”, “It's a girl!”, and “It's a puppy!” When the user clicks any of these buttons, you want to segue to a screen where the user can type in a message. You want the message entry screen to be customized with a theme based on which button was pressed: pink hearts for girls, blue trucks for boys, and a doghouse for puppies.
Give your destination view controller a message for each of these possibilities:
@interface MessageComposerViewController : UIViewController
@property (nonatomic, weak) id<MessageComposerViewControllerDelegate> delegate;
- (void)useGirlTheme;
- (void)useBoyTheme;
- (void)usePuppyTheme;
@end
Then, in your main screen view controller's prepareForSegue:sender:
, you test the identifier and send the appropriate message to the destination view controller:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
MessageComposerViewController *dvc = segue.destinationViewController;
dvc.delegate = self;
if ([segue.identifier isEqualToString:@"Girl"]) {
[dvc useGirlTheme];
}
else if ([segue.identifier isEqualToString:@"Boy"]) {
[dvc useBoyTheme];
}
else if ([segue.identifier isEqualToString:@"Puppy"]) {
[dvc usePuppyTheme];
}
}
Upvotes: 3