展示了如何使用故事板使自定义容器视图控制器管理多个子视图控制器。这个想法基于mluton/EmbeddedSwapping代码。
子视图控制器通过自定义转场与容器连接。自定义转场什么都不做,但存在于故事板中连接事物的目的。自定义容器视图控制器在prepareForSegue:sender
中管理子视图控制器。
带有自定义动画的使用示例
#import "ViewController.h"
#import "ContainerViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
static NSString* const firstControllerSegueIdentifier = @"embedFirst";
static NSString* const secondControllerSegueIdentifier = @"embedSecond";
@interface ViewController ()
@property (nonatomic, weak) ContainerViewController *containerViewController;
- (IBAction)swapButtonPressed:(id)sender;
@end
@implementation ViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"embedContainer"]) {
self.containerViewController = segue.destinationViewController;
self.containerViewController.animationBlock = ^void(UIViewController* container, UIViewController* fromViewController, UIViewController* toViewController) {
[container transitionFromViewController:fromViewController toViewController:toViewController duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
[fromViewController removeFromParentViewController];
[toViewController didMoveToParentViewController:self];
}];
};
}
}
- (IBAction)swapButtonPressed:(id)sender {
if ([self.containerViewController.currentController isKindOfClass:[FirstViewController class]]) {
[self.containerViewController performSegueWithIdentifier:secondControllerSegueIdentifier sender:self];
} else {
[self.containerViewController performSegueWithIdentifier:firstControllerSegueIdentifier sender:self];
}
}
@end
MIT许可证。