UIViewController

AppDelegate.m

#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
-(void)dealloc
{
    [_window release];
    [super dealloc];
}
// 告诉主程序window已经创建完毕
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    [_window release];

    // 创建一个rootviewcontroller对象
    RootViewController *rootVC=[[RootViewController alloc]init];
    // 设置window根视图控制器
    self.window.rootViewController=rootVC;
    [rootVC release];
    // 创建一个viewController时直接创建一个和屏幕一样大小的view 
    return YES;
}

RootVIewController.m

#import "RootViewController.h"
#import "LTVIew.h"
#import "SecondViewController.h"
#define HEIGHT  self.view.frame.size.height
@interface RootViewController ()<UITextFieldDelegate>

@property(nonatomic,retain)NSMutableArray *arr;

@end

@implementation RootViewController
// 有一个初始化方法
// vc 的初始化方法,这个方法一般我们自己就调用了,不需要我们再额外的去调用
// 一般我们会用这个方法来初始化一些容器,比如数组,字典等
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr=[NSMutableArray array];// 创建一个空数组并且进行初始化
    }
    NSLog(@"%s",__FUNCTION__);// 会按照执行方法的顺序打印方法名
      return self;
}

// 加载视图  view指的就是self.view
-(void)loadView
{
   // 如果要重写父类的方法的话  先调用  用super调用父类的方法,保证原功能不变,接下来在super方法下面再写自己要新添加的方法
    [super loadView];
    NSLog(@"%s",__FUNCTION__);
}


#pragma mark   视图加载完成
// 视图加载完成
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor=[UIColor yellowColor];
    NSLog(@"%s",__FUNCTION__);

    //视图的创建和铺设都在viewdidload方法里进行   
    // 铺3个textfield
    // 给每个textfield都设置代理人
    UITextField *textField1=[[UITextField alloc]initWithFrame:CGRectMake(100, 200, 150, 40)];
    [self.view addSubview:textField1];
    textField1.layer.borderWidth=1;
    textField1.layer.borderColor=[UIColor redColor].CGColor;
    textField1.layer.cornerRadius=10;
    textField1.delegate=self;
    [textField1 release];
    UITextField *textField2=[[UITextField alloc]initWithFrame:CGRectMake(100, 300, 150, 40)];
    [self.view addSubview:textField2];
    textField2.layer.borderWidth=1;
    textField2.layer.borderColor=[UIColor redColor].CGColor;
    textField2.layer.cornerRadius=10;
    textField2.delegate=self;
    [textField2 release];

    UITextField *textField3=[[UITextField alloc]initWithFrame:CGRectMake(100, 400, 150, 40)];
    [self.view addSubview:textField3];
    textField3.layer.borderWidth=1;
    textField3.layer.borderColor=[UIColor redColor].CGColor;
    textField3.layer.cornerRadius=10;
    textField3.delegate=self;
    [textField3 release];

    // 铺一个button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame=CGRectMake(100, 500, 150, 40);
    [button setTitle:@"下一页" forState:UIControlStateNormal];
    button.backgroundColor=[UIColor redColor];
    [self.view addSubview:button];
    button.layer.borderWidth=1;
    button.layer.borderColor=[UIColor redColor].CGColor;
    button.layer.cornerRadius=10;
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    button.alpha=0.7;    
}
// 当虚拟键盘弹起挡住控件的时候,使view往上走
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    // 只要输入框被激活,就会触发这个方法
    // 判断
    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;
}

// 等到编译结束的时候,再让他回到原位
-(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);
    }
    NSLog(@"结束的时候会触发");
    return YES;
}

// 随机改变背景颜色
// RGB的范围255
-(void)click:(UIButton *)button
{  [UIColor clearColor];
//    self.view.backgroundColor=[UIColor colorWithRed:arc4random()% 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:0.3];    

 // 跳转到下一个ViewController
    // 创建一个secondVC的对象
    SecondViewController *secondVC=[[SecondViewController alloc]init];

    // 设置一下跳转时候的动画效果   
    [secondVC setModalTransitionStyle:UIModalTransitionStylePartialCurl];   
    // 进行跳转
    [self presentViewController:secondVC animated:YES completion:^{       
    }];
    // 内存管理
    [secondVC release];

}

//视图的创建和铺设都在viewdidload方法里进行
// 铺3个textfield

// 手动加警告
#warning 这个方法是视图将要出现的意思

// 视图将要出现
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"%s",__FUNCTION__);
}

// 视图已经出现
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
     NSLog(@"%s",__FUNCTION__);
}

// 视图将会消失
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
     NSLog(@"%s",__FUNCTION__);
}

// 视图已经消失
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
     NSLog(@"%s",__FUNCTION__);
}

LTView.h 和LTView.m的创建看上一篇博客

SecondViewController.m

#import "SecondViewController.h"
@class RootViewController;
@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    // Do any additional setup after loading the view.

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame=CGRectMake(100, 100, 100, 30);
    button.layer.borderColor=[UIColor redColor].CGColor;
    button.layer.borderWidth=1;
    button.layer.cornerRadius=10;
    [self.view addSubview:button];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];


}

-(void)click:(UIButton *)button
{
    // 点击返回,回到前一个页面
    [self dismissViewControllerAnimated:YES completion:^{

    }]; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值