自定义View中
@implementation Rootview
- (id)initWithFrame:(CGRect)frame
{
selfsuperinitWithFrame:frame];
ifself)
{
self.buttonUIButtonbuttonWithType:UIButtonTypeSystem];
self.button.frameCGRectMake(100, 100, 100, 30);
self.buttonsetTitle:@"跳转"forState:UIControlStateNormal];
selfaddSubview:_button];
self.textFieldUITextFieldalloc]initWithFrame:CGRectMake(100, 200, 200, 30)];
self.textField.borderStyleUIButtonTypeRoundedRect;
self.textField.placeholder@"请输入";
selfaddSubview:_textField];
self.textFieldrelease];
}
returnself;
}
// 重写layoutSubviews
// 当视图被初始化的时候系统会自动调用此方法
// 当视图frame改变的时候系统会自动调用此方法
// 当我们frame改变系统会调用此方法系统内部还会自己再调用一次
- (void)layoutSubviews
{
superlayoutSubviews];
// 获得的是设备的方向
// 方向分为四种
// 1.home建在下建在上建在左建在右
UIInterfaceOrientationUIApplicationsharedApplication].statusBarOrientation;
switch (orientation)
{
caseUIInterfaceOrientationPortrait:
NSLog(@"home建在下");
break;
caseUIInterfaceOrientationPortraitUpsideDown:
NSLog(@"home建在上");
break;
caseUIInterfaceOrientationLandscapeLeft:
NSLog(@"home建在左");
break;
caseUIInterfaceOrientationLandscapeRight:
NSLog(@"home建在右");
break;
default:
break;
}
}
// loadView 系统内部的方法中实现了给控制器的View进行了赋值
// 此时此刻我们进行了重写如果不在自己重写的loadview方法中出现给self.view = xxx 的赋值过程那么之后我们在操作self.view的时候相当于在操作空指针会导致程序崩溃
// 程序的执行顺序如果有loadView 会先走loadView方法再走viewDidLoad的方法然后会走视图将要出现然后再走视图已经出现
- (void)loadView
{
self.rootView = [[Rootview alloc]init];
self.rootView.backgroundColor = [UIColor greenColor];
self.view = _rootView;
[_rootView release];
}
// viewDidLoad 视图被加载的时候会自动调用的方法
//比较常用的是viewDidLoad
- (void)viewDidLoad {
// [super viewDidLoad];
// // Do any additional setup after loading the view.
// // 每一个视图控制器都有一个自己自带的UIView 我们把所有的空间都需要铺放到视图控制器自带的View上
// // 每一个视图控制器在以后我们实际开发中相当于一个界面一个视图控制器对应一个界面
// // 视图控制的主要作用:1.分担AppDelegate的工作量抽离代码更好的实现了界面之间的划分
self.view.backgroundColor = [UIColor redColor];
// UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
// button.frame = CGRectMake(100, 100, 100, 30);
// [button setTitle:@"跳转" forState:UIControlStateNormal];
// [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
// [self.view addSubview:button];
//[button release];
创建自定义视图类的对象
self.rootView = [[Rootview alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
//self.rootView.backgroundColor = [UIColor greenColor];
[self.rootView.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_rootView];
[_rootView release];
self.rootView.textField.delegate = self;
}
#pragma mark ---点击return回收键盘---
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
#pragma mark ---点击空白回收键盘---
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.rootView.textField resignFirstResponder];
}
#pragma mark --- button触发方法---
- (void)buttonAction:(UIButton *)button
{
创建第二个界面的对象从第一个界面跳转到第二个界面
每回点击跳转都需要重新创建一个对象保证对象每一回都是新的
// SecondViewController *secondViewController = [[SecondViewController alloc]init];
// [self presentViewController:secondViewController animated:YES completion:nil];
//
// /*
// [self presentViewController:secondViewController animated:YES completion:^{// 在跳转完成的同时附加的效果
// self.view.backgroundColor = [UIColor greenColor];
// }];
// */
// [secondViewController release];
SecondViewController *secondViewController = [[SecondViewController alloc]init];
[self presentViewController:secondViewController animated:YES completion:nil];
[secondViewController release];
}
#pragma mark ---视图出现或者消失的时候会自动调用的方法---
// 视图将要出现
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
第一个界面将要出现");
}
// 视图已经出现
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
第一个界面已经出现");
}
// 视图将要消失
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
第一个界面将要消失");
}
// 视图已经消失
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
第一个界面已经消失");
}
#pragma mark ---屏幕旋转-----
// 屏幕旋转需要几个步骤
// 1.支持屏幕旋转
// 默认是YES 如果是NO 不支持屏幕旋转
- (BOOL)shouldAutorotate
{
return YES;
}
// 2.支持旋转的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
//- (NSUInteger )supportedInterfaceOrientations
{
支持所有的方向
工程设置里的方向支持我们也要勾选上
//return UIInterfaceOrientationMaskAll;
return UIInterfaceOrientationMaskAllButUpsideDown;
}
// 已经弃用方法
// 设备将要旋转时会自动调用的方法之后已经弃用强打还能够实现
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
设备将要旋转");
}
// 设备已经旋转时会自动调用的方法之后已经弃用强打还能够实现
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
设备已经旋转到目标方向");
}
#pragma mark --- 新的屏幕旋转方法---
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
if (size.width > size.height)
{
横屏");
}
else
{
竖屏");
}
}