码迷,mamicode.com
首页 > 移动开发 > 详细

iOS UI03_UIViewController视图控制器

时间:2015-08-01 10:09:52      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:视图控制器   换页动态效果   ltview   uicontroller   

//

//  AppDelegate.m

//  UI03_UIViewController视图控制器

//

//  Created by dllo on 15/7/31.

//  Copyright (c) 2015 zhozhicheng. All rights reserved.

//


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

    //1.创建一个rootViewController对象

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

    //2.window设置根视图控制器

    self.window.rootViewController=rootVC;

    [rootVC release];


    return YES;

}


- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}


- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}


- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}


- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}


@end










//

//  RootViewController.m

//  UI03_UIViewController视图控制器

//

//  Created by dllo on 15/7/31.

//  Copyright (c) 2015 zhozhicheng. All rights reserved.

//


#import "RootViewController.h"

#define HEIGHT self.view.frame.size.height

//#import "LTView.h"

#import "SecondViewController.h"



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

}

-(void)loadView{

    [super loadView];

    NSLog(@"%s",__FUNCTION__);

    //self.view的加载

}

#pragma mark 视图将要出现

-(void)viewWillAppear:(BOOL)animated

{

 

    [super viewWillAppear:animated];

    NSLog(@"%s",__FUNCTION__);

}

#warning 这个是方法已经出现(自己加的警告)

-(void)viewDidAppear:(BOOL)animated

{

  

    [super viewDidAppear:animated];

    NSLog(@"%s",__FUNCTION__);

}

#pragma mark 视图将要消失

-(void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];

    NSLog(@"%s",__FUNCTION__);

}

#pragma mark 视图已经消失

-(void)viewDidDisappear:(BOOL)animated

{

    [super viewDidAppear:animated];

    NSLog(@"%s",__FUNCTION__);

}

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






- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor=[UIColor cyanColor];

//    NSLog(@"%s",__FUNCTION__);

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

    //铺三个textfield

    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.layer.borderWidth=1;

    button.layer.cornerRadius=10;

    

    

    

    

}

//页面上移

-(BOOL)textFieldShouldBeginEditing:(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;

}

// 等到编译结束的时候,再让他回到原位

-(BOOL)textFieldShouldEndEditing:(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;

}





-(void)click:(UIButton *)button

{

    //创建一个secondViewController的对象

    SecondViewController *secondVC=[[SecondViewController alloc] init];

    //设置一下跳转的动画效果

    [secondVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];

    //进行跳转

    [self presentViewController:secondVC animated:YES completion:^{

        

    }];

    //内存管理

    [secondVC release];

    

    //差生随即颜色

//    [UIColor clearColor];

//    self.view.backgroundColor =[UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0  blue:arc4random() % 256 / 255.0  alpha:1 ];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

//回收键盘

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [textField resignFirstResponder];

    return YES;

}




/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end















//

//  SecondViewController.m

//  UI03_UIViewController视图控制器

//

//  Created by dllo on 15/7/31.

//  Copyright (c) 2015 zhozhicheng. All rights reserved.

//


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

    

    

    

    

    

    

    

    

}

//点击回到前一个页面

-(void)click:(UIButton *)button

{

    [self dismissViewControllerAnimated:YES completion:^{

        

    }];

}






- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end



版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS UI03_UIViewController视图控制器

标签:视图控制器   换页动态效果   ltview   uicontroller   

原文地址:http://blog.csdn.net/cheng_xiansheng/article/details/47183181

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!