码迷,mamicode.com
首页 > 其他好文 > 详细

addChildViewController

时间:2015-05-07 16:08:44      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

View Controller中可以添加多个sub view,在需要的时候显示出来;

可以通过viewController(parent)中可以添加多个child viewController;来控制页面中的sub view,降低代码耦合度;

通过切换,可以显示不同的view;替代之前的addSubView的管理

#import "TheatreListDetailViewController.h"类viewDidLoad里

    self.scrollView.delegate = self;
    self.scrollView.scrollEnabled = YES;
    self.scrollView.pagingEnabled=YES;
    self.scrollView.showsHorizontalScrollIndicator=NO;
    self.scrollView.showsVerticalScrollIndicator=NO;
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.scrollView.clipsToBounds = NO;
    
    self.scrollView.contentSize=CGSizeMake([UIScreen mainScreen].bounds.size.width*3, [UIScreen mainScreen].bounds.size.height-64);
    
    UIView *navView1=[[UIView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width*0, 0, [UIScreen mainScreen].bounds.size.width*1, [UIScreen mainScreen].bounds.size.height-64)];
    TheatreDetailViewController * detailViewController1 = [[TheatreDetailViewController alloc] init];
    [navView1 addSubview:detailViewController1.view];
    [self addChildViewController:detailViewController1];
    [self.scrollView addSubview:navView1];
    
    
    UIView *navView2=[[UIView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width*1, 0, [UIScreen mainScreen].bounds.size.width*1, [UIScreen mainScreen].bounds.size.height-64)];
    SectionViewViewController * detailViewController2 = [[SectionViewViewController alloc] init];
    detailViewController2.contentUrl=@"https://www.baidu.com";
    [navView2 addSubview:detailViewController2.view];
    [self addChildViewController:detailViewController2];
    [self.scrollView addSubview:navView2];
    
    UIView *navView3=[[UIView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width*2, 0, [UIScreen mainScreen].bounds.size.width*1, [UIScreen mainScreen].bounds.size.height-64)];
    CopyrightInformationViewController * detailViewController3 = [[CopyrightInformationViewController alloc] init];
    detailViewController3.contentUrl=@"https://www.baidu.com";
    [navView3 addSubview:detailViewController3.view];
    [self addChildViewController:detailViewController3];
    [self.scrollView addSubview:navView3];

 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (scrollView==_scrollView) {
        int index = fabs(scrollView.contentOffset.x) / scrollView.frame.size.width;
        NSLog(@"index  %d",index);
        if (index==0) {
            //剧情介绍

            [self.scrollView scrollRectToVisible:CGRectMake([UIScreen mainScreen].bounds.size.width*0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) animated:YES];
            
        } else if (index==1){
            //章节浏览

            [self.scrollView scrollRectToVisible:CGRectMake([UIScreen mainScreen].bounds.size.width*1, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) animated:YES];
            
        } else if (index==2){
            //版权信息

            [self.scrollView scrollRectToVisible:CGRectMake([UIScreen mainScreen].bounds.size.width*2, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) animated:YES];
            
        }
        
    }
    
    
}

#import "TheatreDetailViewController.h"类里

        UIButton *aButton=[UIButton buttonWithType:UIButtonTypeCustom];
        aButton.frame=CGRectMake(40,100, 240, 40);
        [aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        aButton.titleLabel.font=[UIFont systemFontOfSize:12];
        aButton.titleLabel.textAlignment=NSTextAlignmentCenter;
        [aButton setBackgroundColor:[UIColor clearColor]];
        [aButton setShowsTouchWhenHighlighted:YES];//按的时候设置高亮
        [self.view addSubview:aButton];

        [aButton setImage:[UIImage imageNamed:@"sz"] forState:UIControlStateNormal];
        [aButton setTitle:@"设置" forState:UIControlStateNormal];
        [aButton addTarget:self action:@selector(setAction:) forControlEvents:UIControlEventTouchUpInside];

 

//设置
-(void)setAction:(UIButton *)sender{

    TheatreSettingViewController * detailViewController = [[TheatreSettingViewController alloc] init];
    [self.navigationController pushViewController:detailViewController animated:YES];

}

addChildViewController

标签:

原文地址:http://www.cnblogs.com/huangzs/p/4484858.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!