-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMBStoryboardLinkSegue.m
76 lines (55 loc) · 2.42 KB
/
MBStoryboardLinkSegue.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
//
// MBStoryboardLinkSegue.m
// MBStoryboardLink
//
// Created by Markus on 26.11.13.
// Copyright (c) 2013 Markus. All rights reserved.
//
#import "MBStoryboardLinkSegue.h"
#import "MBStoryboardLink.h"
@interface MBStoryboardLinkSegue ()
@end
@implementation MBStoryboardLinkSegue
-(id)initWithIdentifier:(NSString *)identifier source:(UIViewController *)source destination:(UIViewController *)destination
{
NSAssert([destination isKindOfClass:[MBStoryboardLink class]], @"MBStoryboardLinkSegue can only be used with a MBStoryboardLink as seque destination.");
// get user defined runtime attributes
NSString *sbName = [(MBStoryboardLink*)destination storyboardName];
NSString *sceneName = [(MBStoryboardLink*)destination sceneIdentifier];
// if failed try to read the contents of a UILabel
if ([sbName length] == 0) {
for (UIView *subView in destination.view.subviews) {
if ([subView isKindOfClass:[UILabel class]]) {
NSString *text = [(UILabel*)subView text];
NSArray *parts = [text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([parts count] >= 2) {
sbName = parts[0];
sceneName = parts[1];
}
break;
}
}
}
NSAssert(sbName, @"Unable to load linked storyboard. MBStoryboardLink storyboardName is nil. Forgot to set attribute in interface builder?");
NSAssert(sceneName, @"Unable to load linked scene. MBStoryboardLink sceneIdentifier is nil. Forgot to set attribute in interface builder?");
// load new destination from storyboard
@try {
UIStoryboard *sb = [UIStoryboard storyboardWithName:sbName bundle:nil];
if ([sceneName length] == 0) {
destination = [sb instantiateInitialViewController];
} else {
destination = [sb instantiateViewControllerWithIdentifier:sceneName];
}
}
@catch (NSException *exception) {
NSLog(@"Failed to follow Storyboardlink('%@') to [%@; %@] from %@", identifier, sbName, sceneName, source);
@throw;
}
// continue init with linked destination viewcontroller
self = [super initWithIdentifier:identifier source:source destination:destination];
if (self) {
_animated = YES;
}
return self;
}
@end