Skip to content

Instantly share code, notes, and snippets.

@akingdom
Last active November 15, 2022 03:38
Show Gist options
  • Save akingdom/722acb58e876f38955a60f852cf73e53 to your computer and use it in GitHub Desktop.
Save akingdom/722acb58e876f38955a60f852cf73e53 to your computer and use it in GitHub Desktop.
A custom iOS segue to fully replace the current UIViewController with a new UIViewController.
// Objective C
// Display initial screen before final UIViewController loads (e.g. Login credentials)
//
// By Andrew Kingdom
// MIT license
//
// Instructions:
//
// On the main storyboard, create your login screen as, say, a subclass of UIViewController.
// Make sure it is the initial scene (check Is Initial View Controller).
//
// On the storyboard, create a new segue from your login screen to the final screen (e.g. SplitViewController).
// Give it an arbitrary identifier, such as 'Load SplitViewController',
// and set the segue custom class name to FullyReplaceSegue.
//
// Finally, in your login class .m file, add code to be called once the user has logged in:
//
// [self performSegueWithIdentifier:@"Load SplitViewController" sender:self];
//
#import <UIKit/UIKit.h>
@protocol FullyReplaceSegueDelegate
@optional
/// The segue will invoke this method on the source UIViewController before removing it from its window. Returned data (or nil) will be passed to didGetDataFromSourceViewController: on the destination UIViewController. Suggestion: return an NSDictionary.
- (NSObject * _Nullable)willPassDataToDestinationViewController;
/// The segue will invoke this method on the destination ViewController after adding it, with any data from willPassDataToDestinationViewController on the outgoing replacement destination ViewController.
- (void)didGetDataFromSourceViewController:(NSObject * _Nullable)data;
@end
@interface FullyReplaceSegue : UIStoryboardSegue
@end
// Objective C
// Display initial screen before final UIViewController loads (e.g. Login credentials)
//
// By Andrew Kingdom
// MIT license
//
#import "FullyReplaceSegue.h"
#import <UIKit/UIKit.h>
@implementation FullyReplaceSegue
- (void)perform {
// Add your own animation code here.
UIViewController *source = self.sourceViewController;
UIViewController *destination = self.destinationViewController;
NSObject * _Nullable data = nil;
if([self.sourceViewController respondsToSelector:
@selector(willPassDataToDestinationViewController)]) {
data = [self.sourceViewController
performSelector: @selector(willPassDataToDestinationViewController)];
}
UIWindow *window = source.view.window;
window.rootViewController = destination;
if([self.sourceViewController respondsToSelector:
@selector(didGetDataFromSourceViewController:)]) {
[self.sourceViewController
performSelector: @selector(didGetDataFromSourceViewController:)
withObject:data];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment