TKRSegueOptions
's new version SegueContext
for Swift is here!
You can pass the objects to destination view controller with UIStoryboardSegue.
#import "NextViewController.h"
#import "ModalViewController.h"
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([@"Next" isEqualToString:segue.identifier]) {
NextViewController *nextViewController = (NextViewController *)segue.destinationViewController;
nextViewController.number = @1;
[self.navigationController pushViewController:nextViewController animated:YES];
} else if ([@"Modal" isEqualToString:segue.identifier]) {
ModalViewController *modalViewController = (ModalViewController *)segue.destinationViewController;
modalViewController.number = @2;
[self.navigationController presentViewController:modalViewController animated:YES completion:nil];
}
}
- You must import the view controller class of the transition destination...
- You must publish the public methods for pass the value...
TKRSegueOptions will solve these problems!
All you need to pass the values when the timing of the view controller transition.
- import at pch
#import "TKRSegueOptions.h"
- Source View Controller
[self performSegueWithIdentifier:@"Modal" options:@888];
- Destination View Controller
self.label.text = self.segueOptions.stringValue; //< @"888"
// Source View Controller
NSDictionary *options = @{
@"url": self.url,
@"shop": self.shop,
};
[self performSegueWithIdentifier:@"Modal" options:options];
// Destination View Controller
NSURL *url = self.segueOptions[@"url"];
// Source View Controller
CustomSegueOption *option = [CustomSegueOption new];
option.number = @888;
option.string = @"hello";
[self performSegueWithIdentifier:@"Modal" options:option];
// Destination View Controller
CustomSegueOption *option = (CustomSegueOption *)presentedViewController.segueOptions;
NSLog(@"number: %@, string: %@", option.number, option.string);
// Implement this method in source view controller
- (TKRSegueOptionSetting *)segueOptionSetting
{
__weak typeof(self) wself = self;
return [TKRSegueOptionSetting settingWithDictionary:@{
@"Embed1": ^{
return wself.leftLabel.text;
},
@"Embed2": ^{
return wself.rightLabel.text;
},
}];
}
- key is UIStoryboard's identifier
- value is block that returns the you want to pass to the transition destination
@implementation CustomSegue
- (void)perform
{
UIViewController *sourceViewController = self.sourceViewController;
UIViewController *viewController;
viewController = [sourceViewController.storyboard instantiateViewControllerWithIdentifier:@"NextViewController"];
// you can use `takeOverSegueOptionsFromViewController:`
[viewController takeOverSegueOptionsFromViewController:self.destinationViewController];
[self.sourceViewController presentViewController:viewController animated:YES completion:nil];
}
@end
- If you implement
prepareForSegue:sender:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// You must call the super
[super prepareForSegue:segue sender:sender];
// Your codes ...
}
- Source View Controller
self.performSegueWithIdentifier("Modal", options: ["title": "TITLE"])
- Destination View Controller
if let title = self.segueOptions?["title"] as? NSString {
self.label.text = title //< "TITLE"
}