UI03_视图控制器
2015-09-21 19:47
525 查看
AppDelegate.h
LTView.h
LTView.m
Appdelegate.h
AppDelegate.m
RootViewController.h
RootViewController.m
SecondViewController.h
SecondViewController.h
LTView.h
#import <UIKit/UIKit.h> @interface LTView : UIView<UITextFieldDelegate> // 因为LTView里的label和textfield需要在外面设置一些内容, 所以需要在.h文件里把他们设置成属性 @property(nonatomic, retain)UILabel *myLabel; @property(nonatomic, retain)UITextField *myTextfield; @end
LTView.m
#import "LTView.h" @implementation LTView // 重写initWithFrame - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 创建label和textfield [self createView]; } return self; } - (void)createView { self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 150, 50)]; self.myLabel.layer.borderWidth = 1; self.myLabel.layer.cornerRadius = 10; // 把label贴在LTView上 [self addSubview:self.myLabel]; [_myLabel release]; // textField self.myTextfield = [[UITextField alloc] initWithFrame:CGRectMake(180, 20, 150, 50)]; self.myTextfield.layer.cornerRadius = 10; self.myTextfield.layer.borderWidth = 1; [self addSubview:self.myTextfield]; [_myTextfield release]; self.myTextfield.clearButtonMode = UITextFieldViewModeAlways; // 点击return回收键盘 self.myTextfield.delegate = self; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [self.myTextfield resignFirstResponder]; return YES; } // 别忘了给属性执行dealloc方法 - (void)dealloc { [_myTextfield release]; [_myLabel release]; [super dealloc]; } @end
Appdelegate.h
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
AppDelegate.m
#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]; // 创建一个视图控制器对象 RootViewController *rootVC = [[RootViewController alloc] init]; // 设置根视图控制器 self.window.rootViewController = rootVC; // 释放 [rootVC release]; return YES; }
RootViewController.h
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
RootViewController.m
#import "RootViewController.h" #import "SecondViewController.h" #import "LTView.h" // 宏定义 #define WIDTH self.view.frame.size.width #define HEIGHT self.view.frame.size.height @interface RootViewController ()<UITextFieldDelegate> @end @implementation RootViewController #pragma mark 这个方法自动的执行, 不需要任何的调用, 我们会在这个方法里初始化一些容器, 比如: 数组, 字典 -(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { NSLog(@"%s", __FUNCTION__); } return self; } - (void)loadView { [super loadView]; NSLog(@"%s", __FUNCTION__); // 加载了self.view } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor yellowColor]; NSLog(@"%g, %g", WIDTH, HEIGHT); NSLog(@"%s", __FUNCTION__); UITextField *firstTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 300, 150, 50)]; firstTextField.layer.borderWidth = 1; firstTextField.layer.cornerRadius = 10; [self.view addSubview:firstTextField]; [firstTextField release]; // firstTextField.delegate = self; UITextField *secondTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 400, 150, 50)]; secondTextField.layer.borderWidth = 1; secondTextField.layer.cornerRadius = 10; [self.view addSubview:secondTextField]; [secondTextField release]; // 设置代理人 secondTextField.delegate = self; // 清除按钮的样式 secondTextField.clearButtonMode = UITextFieldViewModeAlways; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [button setTitle:@"下一页" forState:UIControlStateNormal]; button.layer.cornerRadius = 10; button.layer.borderWidth = 1; [self.view addSubview:button]; [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(100, 500, 100, 50); // LTView对象 LTView *ltView = [[LTView alloc] initWithFrame:CGRectMake(5, 5, WIDTH, 200)]; ltView.backgroundColor = [UIColor redColor]; [self.view addSubview:ltView]; [ltView release]; } - (void)click:(UIButton *)button { SecondViewController *secVC = [[SecondViewController alloc] init]; // 设置模态跳转的动画效果 [secVC setModalTransitionStyle:UIModalTransitionStylePartialCurl]; [self presentViewController:secVC animated:YES completion:^{ }]; // 内存 [secVC release]; } #pragma mark 点击清除按钮会触发这个协议方法 - (BOOL)textFieldShouldClear:(UITextField *)textField { NSLog(@"内容清除"); return YES; } #pragma mark 输入框开始编辑 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { if (textField.frame.origin.y > HEIGHT / 2) { // 计算当前textfield距离屏幕中心的距离 CGFloat height = textField.frame.origin.y - HEIGHT / 2; self.view.center = CGPointMake(self.view.center.x, self.view.center.y - height); } return YES; } #pragma mark 结束编辑的时候执行这个方法 - (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; } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSLog(@"textfield:%@", textField.text); NSLog(@"string:%@", string); return YES; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } #pragma mark 视图将要出现 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; NSLog(@"%s", __FUNCTION__); } #pragma mark 视图已经出现 - (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 viewDidDisappear:animated]; NSLog(@"%s", __FUNCTION__); } #warning 视图控制器从初始化方法开始执行, 然后是loadview, 初始化一个和屏幕等大的view, 接下来是viewDidLoad, 这个方法里我们写视图控件的创建, 然后是viewWillAppear, viewDidAppear, 视图控制器消失的时候会执行viewWillDisappear和viewDidDisappear #warning 前三个方法只会执行一次, 而视图消失和出现只要状态符合, 就会执行 - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. // 内存警告的方法 NSLog(@"%s", __FUNCTION__); NSLog(@"内存警告"); }
SecondViewController.h
#import <UIKit/UIKit.h> @interface SecondViewController : UIViewController @end
SecondViewController.h
#import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [button setTitle:@"返回" forState:UIControlStateNormal]; button.layer.cornerRadius = 10; button.layer.borderWidth = 1; [self.view addSubview:button]; [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(100, 500, 100, 50); } - (void)click:(UIButton *)button { // 返回上一页 [self dismissViewControllerAnimated:YES completion:^{ }]; }
相关文章推荐
- UI_无限滚动相册
- UI03_LTView
- HDU 3276 Graph and Queries [离线+并查集+treap]
- block回调中调用方法的UI更新问题
- IOS 不同的 UIViewController 之间如何跳转
- iOS8下,UIAlertController内存泄露怎么办?
- MVC 用EasyUI
- UI_UIScrollView
- iOS UICollectionView
- UI06_无限滚动的相册
- UI06_UIScrollView
- UI05_手势
- UI05_Control
- UI04_delegate
- UI04_Target-Action
- CALayer的常用属性(笔记与分享)
- UI_Touch
- mapreduce中的sequenceFile类,MapFile解析
- UINavigationController (导航控制器)
- UIWindow & UIWindowLevel笔记