Skip to content

Instantly share code, notes, and snippets.

@Ashton-W
Created November 13, 2014 06:08
Show Gist options
  • Save Ashton-W/f2f7e0e1aa88859c07b5 to your computer and use it in GitHub Desktop.
Save Ashton-W/f2f7e0e1aa88859c07b5 to your computer and use it in GitHub Desktop.
I'm happy with this method of moving models around in segues. For every model that might move around, a protocol is created for assignment, and a segue category method is added to assign it to destination view controllers if they conform to it. `prepareForSegue:` sends a messages for all its models to any segue.
/***
*
* Usage:
* ```
* - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
* {
* [segue prepareWithExampleModel:self.model];
* }
* ```
*/
#import "ExampleModelAssignable.h"
@implementation UIStoryboardSegue (Assignable)
- (void)prepareWithExampleModel:(ExampleModel *)model
{
UIViewController *destination = [self viewControllerForAssignment];
if ([destination conformsToProtocol:@protocol(ExampleModelAssignable)]) {
id<ExampleModelAssignable> vc = (id<ExampleModelAssignable>)destination;
[vc setExampleModel:model];
}
}
- (UIViewController *)viewControllerForAssignment
{
if ([self.destinationViewController isKindOfClass:[UINavigationController class]]) {
return ((UINavigationController *)self.destinationViewController).topViewController;
}
else if ([self.destinationViewController isKindOfClass:[UISplitViewController class]]) {
return ((UISplitViewController *)self.destinationViewController).viewControllers.firstObject;
}
else {
return self.destinationViewController;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment