UIViewController(视图控制器)下的View切换

作用:

UIViewController可以实现创建视图并且切换视图而且有过度效果(就是说UIViewController自带一个View 并且可以控制这个View 所以我们所有的按钮都写在这个View)


步骤:



以下步骤在AppDelegate.m里执行

(1).创建一个rootViewController对象,父类是UIViewController

RootViewController *rootVC = [[RootViewController alloc]init];


(2).window设置根视图控制器并进行内存管理

self.window.rootViewController = rootVC;

[rootVC release];


以下步骤在RootViewController.mviewDidLoad书写

(3).View设置背景颜色

注意:视图的创建和铺设都在viewDidLoad方法里进行

self.view.backgroundColor = [UIColor yellowColor];


(4).3textfield

(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.yself.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


现在要实现几个View的 UIViewController( SecondViewControlle r RootViewController所携带的View的跳转 ) 以下步骤在 SecondViewController.m中进行


(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(10050015040);

[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];


}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值