forked from grgcombs/IntelligentSplitViewController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntelligentSplitViewController.m
More file actions
156 lines (126 loc) · 6.03 KB
/
IntelligentSplitViewController.m
File metadata and controls
156 lines (126 loc) · 6.03 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// IntelligentSplitViewController.m
// From TexLege by Gregory S. Combs (last updated 9/21/10)
//
// Released under the Creative Commons Attribution-ShareAlide 3.0 Unported License
// Please see the included license page for more information.
// In a nutshell, you can use this, just attribute this class to me in your thank you notes or about box.
//
#import "IntelligentSplitViewController.h"
#import <objc/message.h>
@implementation IntelligentSplitViewController
//@synthesize isListening;
- (void)awakeFromNib {
[super awakeFromNib];
//debug_NSLog(@"IntelligentSplitViewController awoke from NIB: %@", self.title);
// self.isListening = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willRotate:)
name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
//debug_NSLog(@"IntelligentSplitViewController loaded: %@", self.title);
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
//debug_NSLog(@"IntelligentSplitViewController unloaded: %@", self.title);
[super viewDidUnload];
}
- (void)dealloc {
/* if (self.isListening) {
self.isListening = NO;
*/
@try {
//debug_NSLog(@"Splitview unloading ??? title = %@", self.title);
[[NSNotificationCenter defaultCenter] removeObserver:self /*forKeyPath:UIApplicationWillChangeStatusBarOrientationNotification */];
[[NSNotificationCenter defaultCenter] removeObserver:self /*forKeyPath:UIApplicationDidChangeStatusBarOrientationNotification */];
}
@catch (NSException * e) {
debug_NSLog(@"IntelligentSplitViewController DE-OBSERVING CRASHED: %@ ... error:%@", self.title, [e description]);
}
// }
[super dealloc];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)willRotate:(id)sender {
if (![self isViewLoaded]) // we haven't even loaded up yet, let's turn away from this place
return;
NSNotification *notification = sender;
if (!notification)
return;
UIInterfaceOrientation toOrientation = [[notification.userInfo valueForKey:UIApplicationStatusBarOrientationUserInfoKey] integerValue];
//UIInterfaceOrientation fromOrientation = [UIApplication sharedApplication].statusBarOrientation;
UITabBarController *tabBar = self.tabBarController;
BOOL notModal = (!tabBar.modalViewController);
BOOL isSelectedTab = [self.tabBarController.selectedViewController isEqual:self];
NSTimeInterval duration = [[UIApplication sharedApplication] statusBarOrientationAnimationDuration];
if (!isSelectedTab || !notModal) { // I don't think we're visible...
[super willRotateToInterfaceOrientation:toOrientation duration:duration];
UIViewController *master = [self.viewControllers objectAtIndex:0];
UIBarButtonItem *button = [super valueForKey:@"_barButtonItem"];
NSObject *theDelegate = (NSObject *)self.delegate;
//UIBarButtonItem *buttonUgly = [[[[[self.viewControllers objectAtIndex:1] viewControllers] objectAtIndex:0] navigationItem] rightBarButtonItem];
if (UIInterfaceOrientationIsPortrait(toOrientation)) {
if (theDelegate && [theDelegate respondsToSelector:@selector(splitViewController:willHideViewController:withBarButtonItem:forPopoverController:)]) {
@try {
UIPopoverController *popover = [super valueForKey:@"_hiddenPopoverController"];
objc_msgSend(theDelegate, @selector(splitViewController:willHideViewController:withBarButtonItem:forPopoverController:), self, master, button, popover);
}
@catch (NSException * e) {
NSLog(@"There was a nasty error while notifyng splitviewcontrollers of an orientation change: %@", [e description]);
}
}
}
else if (UIInterfaceOrientationIsLandscape(toOrientation)) {
if (theDelegate && [theDelegate respondsToSelector:@selector(splitViewController:willShowViewController:invalidatingBarButtonItem:)]) {
@try {
objc_msgSend(theDelegate, @selector(splitViewController:willShowViewController:invalidatingBarButtonItem:), self, master, button);
}
@catch (NSException * e) {
NSLog(@"There was a nasty error while notifyng splitviewcontrollers of an orientation change: %@", [e description]);
}
}
}
}
//debug_NSLog(@"MINE WillRotate ---- sender = %@ to = %d from = %d", [sender class], toOrientation, fromOrientation);
}
/*
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
//debug_NSLog(@"Theirs --- will rotate");
}
*/
- (void)didRotate:(id)sender {
if (![self isViewLoaded]) // we haven't even loaded up yet, let's turn away from this place
return;
NSNotification *notification = sender;
if (!notification)
return;
UIInterfaceOrientation fromOrientation = [[notification.userInfo valueForKey:UIApplicationStatusBarOrientationUserInfoKey] integerValue];
//UIInterfaceOrientation toOrientation = [UIApplication sharedApplication].statusBarOrientation;
UITabBarController *tabBar = self.tabBarController;
BOOL notModal = (!tabBar.modalViewController);
BOOL isSelectedTab = [self.tabBarController.selectedViewController isEqual:self];
if (!isSelectedTab || !notModal) { // I don't think we're visible...
[super didRotateFromInterfaceOrientation:fromOrientation];
}
//debug_NSLog(@"MINE DidRotate ---- sender = %@ from = %d to = %d", [sender class], fromOrientation, toOrientation);
}
/*
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
//debug_NSLog(@"Theirs --- did rotate");
}
*/
@end