addChildViewController

来源:互联网 发布:天猫双11晚会网络直播 编辑:程序博客网 时间:2024/05/21 21:01

1:设置三个vc,设置当前父视图的contentView为第三个的view;

设置中间变量 self.currentVC = thirdVC;

2:点击

如果是当前VC,则不发生变化;

否则进行以下操作;

将当前vc记录以下,记录为oldVC,


            [self transitionFromViewController:self.currentVC toViewController:firstVC duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{
                
            } completion:^(BOOL finished) {
                if (finished) {
                    self.currentVC = firstVC;
                }else{
                    self.currentVC = oldVC;
                }
            }];

如果没有加载完,则self.currentVC = oldVC;,如果加载完,就显示self.currentVC = firstVC;

3:demo代码如下:

#import "ViewController.h"
#import "FirstVC.h"
#import "SecondVC.h"
#import "ThirdVC.h"

@interface ViewController (){
    FirstVC *firstVC;
    SecondVC *secondVC;
    ThirdVC *thirdVC;
}


@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (nonatomic, strong) UIViewController * currentVC;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    firstVC = [[FirstVC alloc] init];
    secondVC = [[SecondVC alloc] init];
    thirdVC = [[ThirdVC alloc] init];
    
    [self addChildViewController:firstVC];
    [self addChildViewController:secondVC];
    [self addChildViewController:thirdVC];
    
    [self.contentView addSubview:thirdVC.view];
    
    self.currentVC = thirdVC;
}
- (IBAction)onClick:(id)sender {
    if (self.currentVC==firstVC && [sender tag]==100001) {
        return;
    }
    if (self.currentVC ==secondVC && [sender tag] ==100002) {
        return;
    }
    
    if (self.currentVC == thirdVC && [sender tag]==100003) {
        return;
    }
    
    UIViewController * oldVC = self.currentVC;
    switch ([sender tag]) {
        case 100001:{
            [self transitionFromViewController:self.currentVC toViewController:firstVC duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{
                
            } completion:^(BOOL finished) {
                if (finished) {
                    self.currentVC = firstVC;
                }else{
                    self.currentVC = oldVC;
                }
            }];
        }
            break;
        case 100002:{
            [self transitionFromViewController:self.currentVC toViewController:secondVC duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{
                
            } completion:^(BOOL finished) {
                if (finished) {
                    self.currentVC = secondVC;
                }else{
                    self.currentVC = oldVC;
                }
            }];
        }
            break;
        case 100003:{
            [self transitionFromViewController:self.currentVC toViewController:thirdVC duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{
                
            } completion:^(BOOL finished) {
                if (finished) {
                    self.currentVC = thirdVC;
                }else{
                    self.currentVC = oldVC;
                }
            }];
        }
            break;
            
        default:
            break;
    }
}


0 0
原创粉丝点击