Lets say i have a ViewController with a textField and a button.
I want to use an unwind segue so i could get the information of the textField to my other viewController
You can easily add a segue programmatically (without using storyboards). In the source view controller header file:
@property (nonatomic, strong) UIStoryboardSegue *segue;
In the source view controller implementation file, set up the segue property accordingly:
self.segue = [UIStoryboardSegue segueWithIdentifier:[NSString stringWithFormat:@"%lu", indexPath.item] source:self destination:[PlayerViewController sharedPlayerViewController] performHandler:^{
// Insert whatever; nothing is needed for a basic segue
}];
Also, in the source view controller, add the destination view controller set up and transition in the prepareForSegue
method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// The code in this method both sets up the destination view controller and transitions to it; you could optionally do set up here only, and then transition in an overridden -(void)perform method. This way is more convenient.
[(PlayerViewController *)segue.destinationViewController setView:[PlayerView sharedPlayerView]];
PHAsset *phAsset = (PHAsset *)AppDelegate.assetsFetchResults[segue.identifier.integerValue];
[AppDelegate.cacheManager requestAVAssetForVideo:phAsset options:AppDelegate.videoOptions resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
[[PlayerView sharedPlayerView] addSubview:[PlayerControls sharedPlayerControls]];
[[PlayerControls sharedPlayerControls] setDelegate:(PlayerViewController *)segue.destinationViewController];
[[PlayerView sharedPlayerView] addSubview:[AppDelegate playerControlsLabel]];
[(PlayerViewController *)segue.destinationViewController setupPlaybackForAsset:asset completion:^{
[((AssetsCollectionViewController *)sender).navigationController presentViewController:(PlayerViewController *)segue.destinationViewController animated:TRUE completion:^{
}];
}];
}];
}
Also, add this line anywhere in the source view controller implementation file to perform the segue:
[self prepareForSegue:self.segue sender:self];
The call to the prepareForSegue
method can be made inside an IBAction handler and anywhere else.
Note that the code that presents the destination view controller references a view controller that is not set up in a storyboard; the project from which this code was taken does not use storyboards for anything. BUT THAT DOESN'T MATTER because the segue code will be the same.
Also note that you do not have to modify the destination view controller in any way, shape or form. To programmatically create an unwind segue (in other words, build on the one-way segue shown here):
@implementation RootNavigationController
- (UIStoryboardSegue*)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier {
return [toViewController segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier];
}
@end
Now, that's what I've done in my own app; here's what someone else has done to create a segue without using the storyboard (vs. not having a storyboard at all):
- (void)presentSignupViewController {
// Storyboard ID
UIStoryboard *modalStoryboard = [UIStoryboard storyboardWithName:@"MyStoryboard" bundle:nil];
UINavigationController *navController = [modalStoryboard instantiateViewControllerWithIdentifier:@"MySignupViewController"];
MySignupViewController *controller = [navController viewControllers][0];
// Configure your custom view controller, e.g. setting delegate
controller.delegate = self;
// Show VC
navController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
BlurryModalSegue *segue = [[BlurryModalSegue alloc] initWithIdentifier:@"SignupScene" source:self destination:navController];
[segue perform];
}
The difference is only in the use of perform and prepareForSegue: when using no storyboard at all, you have to call your transition method of choice (push... or present...) in the performForSegue method; however, the transition method is called for you by the perform method otherwise.