您的位置:首页 > 产品设计 > UI/UE

UI03_UIViewController视图控制器

2015-08-06 08:37 405 查看
//
//  AppDelegate.m
//  UI03_UIViewController视图控制器
//
//  Created by dllo on 15/7/31.
//  Copyright (c) 2015年 Clare. 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
//
//  LTView.h
//  UI03_LTView
//
//  Created by dllo on 15/7/31.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface LTView : UIView<UITextFieldDelegate>

// 因为要在类的外部获取输入框的内容,修改label的标题,所以我们可以把这俩部分作为属性写在.h里面,这样在外部可以直接进行修改和设置
@property(nonatomic, retain)UILabel *myLabel;
@property(nonatomic, retain)UITextField *myTextField;

@end


//
//  LTView.m
//  UI03_LTView
//
//  Created by dllo on 15/7/31.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import "LTView.h"

@implementation LTView

// 重写默认的初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 模块化
[self createView];
}
return self;
}

- (void)createView
{
// 创建两个子视图,一个是label,一个是textfield
self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 100, 30)];
self.myLabel.backgroundColor = [UIColor lightGrayColor];
[self addSubview:self.myLabel];
[_myLabel release];

self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(150, 20, 100, 40)];
self.myTextField.backgroundColor = [UIColor lightGrayColor];
[self addSubview:self.myTextField];
// 设置代理人
self.myTextField.delegate = self;
[_myTextField release];
}

- (void)dealloc
{
[_myTextField release];
[_myLabel release];
[super dealloc];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

@end


//
//  RootViewController.m
//  UI03_UIViewController视图控制器
//
//  Created by dllo on 15/7/31.
//  Copyright (c) 2015年 Clare. 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 如果想重写父类的方法,首先先用supper去调用父类的方法,这样可以保证原功能不变,然后在方法里再写新添加的功能

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

// 铺3个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];

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
{

// 只要输入框被激活,就会触发这个方法
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;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
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:0.5];
//    NSLog(@"1");

// 创建一个secondVC的对象
SecondViewController *secondVC = [[SecondViewController alloc] init];
// 设置一下跳转的时候动画效果
[secondVC setModalTransitionStyle: UIModalTransitionStyleFlipHorizontal];

// 进行跳转
[self presentViewController:secondVC animated:YES completion:^{

}];
// 内存管理
[secondVC release];

}

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

}
#pragma mark 视图将要出现

// 视图已经出现
#warning 这个方法是视图已经出现
- (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__);
}

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


//
//  SecondViewController.m
//  UI03_UIViewController视图控制器
//
//  Created by dllo on 15/7/31.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

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

}];

}

- (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;
button.layer.masksToBounds = YES;
[self.view addSubview:button];
[button setTitle:@"返回" forState:UIControlStateNormal];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}

- (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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: