UIViewController可以实现创建视图并且切换视图而且有过度效果(就是说UIViewController自带一个View 并且可以控制这个View 所以我们所有的按钮都写在这个View)
步骤:
以下步骤在AppDelegate.m里执行
(1).创建一个rootViewController对象,父类是UIViewController
RootViewController *rootVC = [[RootViewController alloc]init];
(2).给window设置根视图控制器并进行内存管理
self.window.rootViewController = rootVC;
[rootVC release];
以下步骤在RootViewController.m里viewDidLoad书写
(3).View设置背景颜色
注意:视图的创建和铺设都在viewDidLoad方法里进行
self.view.backgroundColor = [UIColor yellowColor];
(4).铺3个textfield
(7).给三个按钮设置代理人
UITextField *textField1 = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 150, 40)];
textField1.layer.borderWidth = 1;
textField1.layer.cornerRadius = 10;
[self.view addSubview:textField1];
textField1.delegate = self ;// (7)
[textField1 release];
UITextField *textField2 = [[UITextField alloc]initWithFrame:CGRectMake(100, 300, 150, 40)];
textField2.layer.borderWidth = 1;
textField2.layer.cornerRadius = 10;
[self.view addSubview:textField2];
textField2.delegate = self ;// (7)
[textField2 release];
UITextField *textField3 = [[UITextField alloc]initWithFrame:CGRectMake(100, 400, 150, 40)];
textField3.layer.borderWidth = 1;
textField3.layer.cornerRadius = 10;
[self.view addSubview:textField3];
textField3.delegate = self ;// (7)
[textField3 release];
(5).铺一个button并调用方法(功能键用于调用随机颜色或者切换到另一张视图)
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 500, 150, 40);
[button setTitle:@"下一页" forState:UIControlStateNormal];
[self.view addSubview:button];
button.layer.borderWidth = 1;
button.layer.cornerRadius = 10;
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
注意:在运行之后弹出的键盘会把textField3挡住,(6)以后要解决这个问题
(6).签订协议
@interface RootViewController ()<UITextFieldDelegate>
(8).设置这个使整个View升高的方法方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
只要输入框被激活,就会触发这个方法(点一下就被激活)
if (textField.frame.origin.y > HEIGHT / 2) {只要输入框在屏幕高度的一半以下
先做一个差值
CGFloat height = textField.frame.origin.y - self.view.frame.size.height / 2;
self.view.center = CGPointMake(self.view.center.x, self.view.center.y - height);
}
return YES;
}
(9).等到编译结束的时候让View回复原来的位置
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
注意:整个是在移动self.view,父视图的移动会让所有的子视图一同移动,而且相对父视图的左边位置不会发生变化,所以,可以沿用上一个方法的判断
if (textField.frame.origin.y > HEIGHT / 2) {
先做一个差值
CGFloat height = textField.frame.origin.y - HEIGHT / 2;
self.view.center = CGPointMake(self.view.center.x, self.view.center.y + height);
}
return YES;
}
现在要实现视图转换功能(都是UIViewController的跳转)
首先需要再创建一个类SecondViewController继承于UIViewController
(1).创建一个button
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 200, 100, 30);
button.backgroundColor = [UIColor yellowColor];
self.view.backgroundColor = [UIColor cyanColor];
button.layer.borderWidth = 1;
button.layer.cornerRadius = 10;
[self.view addSubview:button];
(2).输入文本和调用方法
[button setTitle:@"返回" forState:UIControlStateNormal];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
(3).点击返回,回到前一页面
-(void)click:(UIButton *)button
{
[self dismissViewControllerAnimated:YES completion:^{}];
}
以下步骤在RootViewController.m里进行
首先引头文件
#import "SecondViewController.h"
(4).铺一个button并调用方法(功能键用于调用随机颜色或者切换到另一张视图)该步骤要写在- (void)viewDidLoad方法内
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 500, 150, 40);
[button setTitle:@"下一页" forState:UIControlStateNormal];
[self.view addSubview:button];
button.layer.borderWidth = 1;
button.layer.cornerRadius = 10;
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
- (void)click:(UIButton *)button
{(5).创建一个SecondViewController的对象
SecondViewController *secondVC = [[SecondViewController alloc]init];
(6).设置跳转时候的动画效果
[secondVC setModalTransitionStyle:UIModalTransitionStylePartialCurl];
(7).进行跳转
[self presentViewController:secondVC animated:YES completion:^{}];
(8).内存管理
[secondVC release];
}