AJ学IOS 之控制器view显示中view的父子关系及controller的父子关系_解决屏幕旋转不能传递事件问题

AJ分享,必须精品

一:效果

这里写图片描述

二:项目代码

这个Demo用的几个控制器分别画了不通的xib,随便拖拽了几个空间,主要是几个按钮的切换,主要代码展示下:

//
//  NYViewController.m
//  控制器的view的显示
//
//  Created by apple on 14-10-10.
//  Copyright (c) 2014年 heima. All rights reserved.
//

#import "NYViewController.h"
#import "NYTestViewController.h"

#import "NYOneViewController.h"
#import "NYTwoViewController.h"
#import "NYThreeViewController.h"

@interface NYViewController ()
- (IBAction)vc1;
- (IBAction)vc2;
- (IBAction)vc3;
@property (nonatomic, strong) NYTestViewController *test;

@property (nonatomic, strong) NYOneViewController *one;
@property (nonatomic, strong) NYTwoViewController *two;
@property (nonatomic, strong) NYThreeViewController *three;
@end

@implementation NYViewController

- (NYOneViewController *)one
{
    if (!_one) {
        self.one = [[NYOneViewController alloc] init];
        self.one.view.frame = CGRectMake(10, 70, 300, 300);
    }
    return _one;
}

- (NYTwoViewController *)two
{
    if (!_two) {
        self.two = [[NYTwoViewController alloc] init];
        self.two.view.frame = CGRectMake(10, 70, 300, 300);
    }
    return _two;
}

- (NYThreeViewController *)three
{
    if (!_three) {
        self.three = [[NYThreeViewController alloc] init];
        self.three.view.frame = CGRectMake(10, 70, 300, 300);
    }
    return _three;
}

/**
 * 即将旋转到某个屏幕时调用
 */
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    NSLog(@"NYViewController---willRotateToInterfaceOrientation");
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    NSLog(@"NYViewController---didRotateFromInterfaceOrientation");
}

- (void)viewDidLoad
{
    [super viewDidLoad];

//    NYTestViewController *test = [[NYTestViewController alloc] init];
//    test.view.frame = CGRectMake(100, 100, 200, 300);
//    test.view.backgroundColor = [UIColor redColor];
//    [self.view addSubview:test.view];
//    self.test = test;

    // 如果发现:控制器的view还在,但是view上面的数据不显示,极大可能是因为:控制器被提前销毁了

    // 1.一个控制器的view是可以随意调整尺寸和位置的
    // 2.一个控制器的view是可以随意添加到其他view中
    // 3.如果将一个控制器的view,添加到其他view中显示,那么要想办法保证控制器不被销毁
    // 4.原则:只要view在,view所在的控制器必须得在,这样才能保证view内部的数据和业务逻辑正常
}

- (IBAction)vc1 {
    [self.two.view removeFromSuperview];
    [self.three.view removeFromSuperview];
    [self.view addSubview:self.one.view];
}

- (IBAction)vc2 {
    [self.one.view removeFromSuperview];
    [self.three.view removeFromSuperview];
    [self.view addSubview:self.two.view];
}

- (IBAction)vc3 {
    [self.two.view removeFromSuperview];
    [self.one.view removeFromSuperview];
    [self.view addSubview:self.three.view];
}
@end

三:旋转事件问题

这样貌似就可以完成大多数的需求了,但是有时候我们会发现一些问题,比如当屏幕旋转的时候事件无法传递

/**
 * 即将旋转到某个屏幕时调用
 */
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    NSLog(@"NYViewController---willRotateToInterfaceOrientation");
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    NSLog(@"NYViewController---didRotateFromInterfaceOrientation");
}

如果我们将这两个方法写到one two three这三个控制器中,相应的在屏幕旋转的时候,只有主控制器打印了这个方法,然而其他的控制器中并没有,这里的原因就是他们的控制器是平级的,虽然view是父子关系,解决办法就是设置controller的父子关系。

四:解决代码

当控制器的view互为父子关系,那么控制器最好也互为父子关系

  NYOneViewController *one = [[NYOneViewController alloc]init];

让one控制器成为当前self(HWViewController)的子控制器

    [self addChildViewController:one];

通过关addChildViewController添加一个子控制器,那么这个控制器就会被放到childViewControllers数组中
只要self在,childViewControllers数组就在数组里面的子控制器就在

//
//  NYViewController.m
//  控制器的view的显示
//
//  Created by apple on 14-10-10.
//  Copyright (c) 2014年 heima. All rights reserved.
//

#import "NYViewController.h"
#import "NYTestViewController.h"

#import "NYOneViewController.h"
#import "NYTwoViewController.h"
#import "NYThreeViewController.h"

@interface NYViewController ()
- (IBAction)vc1;
- (IBAction)vc2;
- (IBAction)vc3;
@property (nonatomic, strong) NYTestViewController *test;

@end
@implementation NYViewController


/**
 * 即将旋转到某个屏幕时调用
 */
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    NSLog(@"NYViewController---willRotateToInterfaceOrientation");
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    NSLog(@"NYViewController---didRotateFromInterfaceOrientation");
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    //当控制器的view互为父子关系,那么控制器最好也互为父子关系
    NYOneViewController *one = [[NYOneViewController alloc]init];
    //让one控制器成为当前self(HWViewController)的子控制器
    [self addChildViewController:one];

    //通过关addChildViewController添加一个子控制器,那么这个控制器就会被放到childViewControllers数组中
    //只要self在,childViewControllers数组就在数组里面的子控制器就在

    NYTwoViewController *two = [[NYTwoViewController alloc]init];
    [self addChildViewController:two];

    NYThreeViewController *three = [[NYThreeViewController alloc]init];
    [self addChildViewController:three];
}




- (IBAction)vc1 {
    NYOneViewController *one = self.childViewControllers[0];
    NYTwoViewController *two = self.childViewControllers[1];
    NYThreeViewController *three = self.childViewControllers[2];

    [two.view removeFromSuperview];
    [three.view removeFromSuperview];
    [self.view addSubview:one.view];
}

- (IBAction)vc2 {
    NYOneViewController *one = self.childViewControllers[0];
    NYTwoViewController *two = self.childViewControllers[1];
    NYThreeViewController *three = self.childViewControllers[2];

    [one.view removeFromSuperview];
    [three.view removeFromSuperview];
    [self.view addSubview:two.view];
}

- (IBAction)vc3 {
    NYOneViewController *one = self.childViewControllers[0];
    NYTwoViewController *two = self.childViewControllers[1];
    NYThreeViewController *three = self.childViewControllers[2];

    [two.view removeFromSuperview];
    [one.view removeFromSuperview];
    [self.view addSubview:three.view];
}
@end
原文地址:https://www.cnblogs.com/luolianxi/p/4990290.html