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

http://www.2cto.com/kf/201509/442366.html

一:效果

这里写图片描述

二:项目代码

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<code class = "hljs" objectivec= "" > //
//  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
</code>

三:旋转事件问题

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

?
1
2
3
4
5
6
7
8
9
10
11
12
<code class = "hljs" java= "" > /**
  * 即将旋转到某个屏幕时调用
  */
- ( void )willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
     NSLog( @NYViewController ---willRotateToInterfaceOrientation);
}
 
- ( void )didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
     NSLog( @NYViewController ---didRotateFromInterfaceOrientation);
}</code>

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

四:解决代码

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

?
1
<code class = "hljs" fix= "" >  NYOneViewController *one = [[NYOneViewController alloc]init];</code>

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

?
1
<code class = "hljs" ini= "" >    [self addChildViewController:one];</code>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<code class = "hljs" objectivec= "" > //
//  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
</code>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值