-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUIStoryboardSegue+Link.m
125 lines (101 loc) · 4.25 KB
/
UIStoryboardSegue+Link.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// UIStoryboardSegue+Link.m
// ScriptChart
//
// Created by Ragu Vijaykumar on 5/18/13.
// Copyright (c) 2013 Script Medical. All rights reserved.
//
#import "UIStoryboardSegue+Link.h"
#import "JRSwizzle.h"
#import "NSString+Helper.h"
@interface UIStoryboardSegue (LinkPrivate)
- (id)initWithLinkIdentifier:(NSString*)identifier source:(UIViewController*)source destination:(UIViewController*)destination;
@end
@implementation UIStoryboardSegue (Link)
static NSString* kStoryboardName = @"";
static NSString* kSceneIdentifier = @"";
static NSString* const kDynamicStoryboardLink = @"DynamicSL";
static NSString* const kStaticStoryboardLink = @"StaticSL";
static NSString* const kStaticStoryboardLinkSeperator = @"-";
- (id)initWithLinkIdentifier:(NSString*)identifier source:(UIViewController*)source destination:(UIViewController*)destination {
#ifdef DEBUG
NSLog(@"Segue Identifier: %@", identifier);
#endif
if([identifier contains:kDynamicStoryboardLink]) {
NSAssert([kStoryboardName length], @"No storyboard name");
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:kStoryboardName bundle:nil];
if ([kSceneIdentifier length] == 0)
destination = [storyboard instantiateInitialViewController];
else
destination = [storyboard instantiateViewControllerWithIdentifier:kSceneIdentifier];
NSAssert(destination,
@"No scene found in storyboard: \"%@\" with optional identifier: \"%@\"",
kStoryboardName,
kSceneIdentifier);
#ifdef DEBUG
if ([kSceneIdentifier length] == 0)
NSLog(@"Dynamic segue to storyboard %@", kStoryboardName);
else
NSLog(@"Dynamic segue to scene %@ in storyboard %@", kSceneIdentifier, kStoryboardName);
#endif
} else if([identifier contains:kStaticStoryboardLink]) {
// Parse identifier to get storyboard name and scene identifier
NSArray* components = [identifier componentsSeparatedByString:kStaticStoryboardLinkSeperator];
if([components count] > 1)
kStoryboardName = components[1];
else
kStoryboardName = @"";
if([components count] > 2)
kSceneIdentifier = components[2];
else
kSceneIdentifier = @"";
NSAssert([kStoryboardName length], @"No storyboard name");
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:kStoryboardName bundle:nil];
if ([kSceneIdentifier length] == 0)
destination = [storyboard instantiateInitialViewController];
else
destination = [storyboard instantiateViewControllerWithIdentifier:kSceneIdentifier];
NSAssert(destination,
@"No scene found in storyboard: \"%@\" with optional identifier: \"%@\"",
kStoryboardName,
kSceneIdentifier);
#ifdef DEBUG
if ([kSceneIdentifier length] == 0)
NSLog(@"Static segue to storyboard %@", kStoryboardName);
else
NSLog(@"Static segue to scene %@ in storyboard %@", kSceneIdentifier, kStoryboardName);
#endif
}
kStoryboardName = @"";
kSceneIdentifier = @"";
return [self initWithLinkIdentifier:identifier source:source destination:destination];
}
+ (void)setStoryboardName:(NSString*)storyboardName {
if(!storyboardName)
storyboardName = @"";
kStoryboardName = [storyboardName copy];
}
+ (void)setScene:(NSString*)sceneIdentifier {
if(!sceneIdentifier)
sceneIdentifier = @"";
kSceneIdentifier = [sceneIdentifier copy];
}
+ (BOOL)link {
static BOOL didSwizzle = NO;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSError* error;
didSwizzle = [[self class] jr_swizzleMethod:@selector(initWithIdentifier:source:destination:)
withMethod:@selector(initWithLinkIdentifier:source:destination:)
error:&error];
#ifdef DEBUG
if(!didSwizzle) {
NSLog(@"Failed link with %@", error);
} else {
NSLog(@"Link succeeded on %@", [self class]);
}
#endif
});
return didSwizzle;
}
@end