0
点赞
收藏
分享

微信扫一扫

iOS ChildViewController详解

#import "ParentViewController.h"
#import "ChildOneViewController.h"
#import "ChildTwoViewController.h"
#import "ChildThreeViewController.h"

@interface ParentViewController () {
    NSInteger _currentViewControllerIndex;
    IBOutlet UIView *_contentChildView;
}

@end

@implementation ParentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil  bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    ChildOneViewController *childOneViewController = [[ChildOneViewController alloc] initWithNibName:@"ChildOneViewController" bundle:nil];
    ChildTwoViewController *childTwoViewController = [[ChildTwoViewController alloc] initWithNibName:@"ChildTwoViewController" bundle:nil];
    ChildThreeViewController *childThreeViewController = [[ChildThreeViewController alloc] initWithNibName:@"ChildThreeViewController" bundle:nil];
    
    [self addChildViewController:childOneViewController];
    [self addChildViewController:childTwoViewController];
    [self addChildViewController:childThreeViewController];
        
    [_contentChildView addSubview:childOneViewController.view];
    _currentViewControllerIndex = 0;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)buttonDidPress:(id)sender
{
//    if (_currentViewControllerIndex == [sender tag]) {
//        return;
//    }
    
    if (_currentViewControllerIndex == [sender tag]) {
        return;
    }
    
    [self transitionFromViewController:[self.childViewControllers objectAtIndex:_currentViewControllerIndex]
                      toViewController:[self.childViewControllers objectAtIndex:[sender tag]]
                              duration:1
                               options:UIViewAnimationOptionTransitionFlipFromLeft
                            animations:^{}
                            completion:^(BOOL finished) {
                                if (finished) {
                                    _currentViewControllerIndex = [sender tag];
                                }
                            }];
}

@end

举报

相关推荐

0 条评论