AJ学IOS 之控制器view显示中view的父子关系及controller的父子关系_解决屏幕旋转不能传递事件问题
来源:互联网 发布:fgo下载完整数据卡住 编辑:程序博客网 时间:2024/05/22 13:14
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
0 0
- AJ学IOS 之控制器view显示中view的父子关系及controller的父子关系_解决屏幕旋转不能传递事件问题
- 猫猫学iOS 之控制器view显示中view的父子关系及controller的父子关系_解决屏幕旋转不能传递事件问题
- iOS 之控制器view显示中view的父子关系及controller的父子关系_解决屏幕旋转不能传递事件问题
- View Controller 的父子关系处理(Custom Container View Controller)
- iOS 【两个控制器的view互为父子关系,那么这两个控制器一定也要互为父子关系】
- iOS 控制器父子关系的建立原则
- 13.控制器的父子关系
- iOS 抽屉效果实现 注意点 及 iOS 控制器间的父子关系 详解
- 控制器父子关系的建立原则
- 视图(view)、视图控制器(view controller)之间的数量关系
- [IOS]创建控制器View的优先级关系
- IOS开发UI基础:控制器父子关系
- hibernate的父子关系及inverse
- hibernate的父子关系及inver
- Hibernate中父子关系对象的删除
- Qt中父子widget的事件传递
- Qt中父子widget的事件传递
- 父子View的wrap,fill
- AJ学IOS 之CoreLocation指南针小应用
- AJ学IOS 之CoreLocation地理编码小Demo输入城市得到经纬度
- eclipse添加ADT插件步骤
- AJ学IOS 之CoreLocation反地理编码小Demo输入经纬度得到城市
- AJ学IOS 之UIDynamic重力、弹性碰撞吸附等现象
- AJ学IOS 之控制器view显示中view的父子关系及controller的父子关系_解决屏幕旋转不能传递事件问题
- AJ学IOS 之微博项目实战(9)微博模型之时间相关重要操作,判断刚刚,昨天,今年等等
- linux shell中的变量PS1------定制linux shell提示符
- AJ学IOS 之微博项目实战(10)微博cell中图片的显示以及各种填充模式简介
- AJ学IOS 之微博项目实战(11)发送微博自定义TextView实现带占位文字
- AJ学IOS 之微博项目实战(12)发送微博自定义工具条代理实现点击事件
- AJ学IOS 之微博项目实战(13)发送微博调用相机里面的图片以及调用相机
- AJ学IOS 之BLOCK的妙用_利用block实现链式编程
- AJ学IOS 之tableView的下拉放大图片的方法