Skip to content

Commit d93cec6

Browse files
committed
Fixing an issue where re-setting the same view controller object would remove the view controller.
1 parent f2cd496 commit d93cec6

File tree

1 file changed

+54
-43
lines changed

1 file changed

+54
-43
lines changed

GRKContainerViewController/GRKContainerViewController.m

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -62,54 +62,65 @@ - (void)setup
6262

6363
- (void)setViewController:(UIViewController *)viewController animated:(BOOL)animated completion:(void(^)(UIViewController *viewController))completion
6464
{
65-
//Ensure the view is loaded
66-
[self view];
67-
68-
//We will use a simple cross fade animation, as indicated
69-
NSTimeInterval duration = animated ? self.transitionAnimationDuration : 0.0f;
70-
71-
// We're fading out views, so make sure they're not invisible when we start.
72-
self.viewController.view.alpha = 1.0f;
73-
viewController.view.alpha = 1.0f;
74-
75-
//Add the new view controller to the view and view controller hierarchies
76-
if (viewController)
65+
if ([self.viewController isEqual:viewController])
7766
{
78-
[self addChildViewController:viewController];
79-
80-
// Add the new view below the current view, and then fade out the current view.
81-
[self.view addSubview:viewController.view];
82-
[self.view insertSubview:viewController.view belowSubview:self.viewController.view];
83-
[viewController didMoveToParentViewController:self];
84-
85-
//Setup constraints to keep the new view pinned to our size.
86-
UIView *containedView = viewController.view;
87-
containedView.translatesAutoresizingMaskIntoConstraints = NO;
88-
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[containedView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(containedView)]];
89-
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containedView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(containedView)]];
90-
}
91-
92-
// Hold onto the old controller, and set the new one.
93-
UIViewController *oldViewController = self.viewController;
94-
_viewController = viewController;
95-
96-
[UIView animateWithDuration:duration animations:^{
97-
// Animate away the old controller view.
98-
oldViewController.view.alpha = 0.0f;
99-
} completion:^(BOOL finished) {
100-
// Quick transitions can leave us trying to remove the view that has just been added. Only remove it if its not the same controller.
101-
if (_viewController != oldViewController) {
102-
[oldViewController willMoveToParentViewController:nil];
103-
[oldViewController.view removeFromSuperview];
104-
[oldViewController removeFromParentViewController];
105-
}
106-
10767
if (completion)
10868
{
109-
//Call the completion block with the new view controller
69+
//Call the completion block with the view controller
11070
completion(viewController);
11171
}
112-
}];
72+
}
73+
else
74+
{
75+
//Ensure the view is loaded
76+
[self view];
77+
78+
//We will use a simple cross fade animation, as indicated
79+
NSTimeInterval duration = animated ? self.transitionAnimationDuration : 0.0f;
80+
81+
// We're fading out views, so make sure they're not invisible when we start.
82+
self.viewController.view.alpha = 1.0f;
83+
viewController.view.alpha = 1.0f;
84+
85+
//Add the new view controller to the view and view controller hierarchies
86+
if (viewController)
87+
{
88+
[self addChildViewController:viewController];
89+
90+
// Add the new view below the current view, and then fade out the current view.
91+
[self.view addSubview:viewController.view];
92+
[self.view insertSubview:viewController.view belowSubview:self.viewController.view];
93+
[viewController didMoveToParentViewController:self];
94+
95+
//Setup constraints to keep the new view pinned to our size.
96+
UIView *containedView = viewController.view;
97+
containedView.translatesAutoresizingMaskIntoConstraints = NO;
98+
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[containedView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(containedView)]];
99+
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containedView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(containedView)]];
100+
}
101+
102+
// Hold onto the old controller, and set the new one.
103+
UIViewController *oldViewController = self.viewController;
104+
_viewController = viewController;
105+
106+
[UIView animateWithDuration:duration animations:^{
107+
// Animate away the old controller view.
108+
oldViewController.view.alpha = 0.0f;
109+
} completion:^(BOOL finished) {
110+
// Quick transitions can leave us trying to remove the view that has just been added. Only remove it if its not the same controller.
111+
if (_viewController != oldViewController) {
112+
[oldViewController willMoveToParentViewController:nil];
113+
[oldViewController.view removeFromSuperview];
114+
[oldViewController removeFromParentViewController];
115+
}
116+
117+
if (completion)
118+
{
119+
//Call the completion block with the new view controller
120+
completion(viewController);
121+
}
122+
}];
123+
}
113124
}
114125

115126
#pragma mark - Accessors

0 commit comments

Comments
 (0)