UIViewController视图控制器

UIViewController视图控制器

import “AppDelegate.h”

import “RootViewController.h”

@interface AppDelegate ()

@end

@implementation AppDelegate

  • (void)dealloc
    {
    [_window release];
    [super dealloc];
    }

  • (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];

    // MVC–ModelViewController
    // 1.创建一个RootViewController对象
    RootViewController *rootVC=[[RootViewController alloc] init];
    // 2.给window设置根视图控制器
    self.window.rootViewController=rootVC;
    [rootVC release];

import “RootViewController.h”

define HEIGHT self.view.frame.size.height

import “SecondViewController.h”

@interface RootViewController ()

@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;
}

-(void)loadView{
[super loadView];
NSLog(@”%s”,FUNCTION);
// self.view 的加载
}

pragma mark 如果想重写父类的方法,首先先用super去调用父类的方法,这样可以保证原功能不变,然后在方法里写新添加的功能

// 视图完成加载
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor whiteColor];
NSLog(@”%s”,FUNCTION);
// 视图的创建和铺设都在viewdidload方法里进行
// 铺3个

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

// 铺一个button
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
button.frame=CGRectMake(100, 500, 150, 40);
[button setTitle:@"下一页" forState:UIControlStateNormal];
[self.view addSubview:button];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
button.titleLabel.font=[UIFont systemFontOfSize:20];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

}

// 往上走
-(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);
}
return YES;
}

-(void)click:(UIButton *)button{
// self.view.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:arc4random()%256/255.0];

// 创建一个secondVC对象
SecondViewController *secondVC=[[SecondViewController alloc] init];
// 设置一下跳转动画效果
[secondVC setModalTransitionStyle:UIModalTransitionStylePartialCurl];
// 进行跳转
[self presentViewController:secondVC animated:YES completion:^{}];

// 内存管理
[secondVC release];

}

// 视图将要出现

pragma mark 视图将要出现

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSLog(@”%s”,FUNCTION);
}
// 视图已经出现
// #warning 这个方法是视图应经出现的意思
-(void)viewDidAppear:(BOOL)animated{
[super viewDidDisappear:animated];
NSLog(@”%s”,FUNCTION);
}
// 视图将要消失
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
NSLog(@”%s”,FUNCTION);
}
// 视图已经消失
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
NSLog(@”%s”,FUNCTION);
}

// 监控内存信息
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

import “SecondViewController.h”

@interface SecondViewController ()

@end

@implementation SecondViewController

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

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值