iOS UI03_UIViewController视图控制器

//

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

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

时间: 2024-11-03 22:44:56

iOS UI03_UIViewController视图控制器的相关文章

IOS开发—视图控制器左边缘右滑pop出栈

IOS视图控制器左边缘右滑pop出栈 IOS7.0之后,苹果优化了一个小功能,就是对于UINavagationController堆栈里的UIViewController,只要轻轻在视图控制器的左边缘右滑一下,该视图控制器就会pop出栈(前提当然是对于非根视图控制器而言).实现方法很简单,一句话搞定: self.navigationController.interactivePopGestureRecognizer.enabled = YES; 事实上对于一个视图控制器而言,该属性的默认值即为Y

【IOS开发—视图控制器】

一.UIViewController 视图控制器是UIViewController类或者其子类对象,每个视图控制器都负责管理一个视图层次结构.在UIViewController中有一个重要的UIView属性, 这个view就是这个视图层的根视图,当程序将view作为子视图加入窗口时,也会加入UIViewController对象所管理的整个视图层次结构. 视图控制器只有在将某个视图显示在屏幕上时,相应的视图控制器才会创建其视图,这种延迟加载视图的做法能提高内存的使用效率.视图控制器创建视图的方式有

IOS开发--视图控制器和基础视图

一.视图控制器 UIViewController的介绍: UIViewController即视图控制器,它在MVC(Model View Controller)模式中充当控制者的角色,它负责控制视图的显示,以及响应用户在该视图中所有可能的动作.使用视图控制器,我们可以很方便的管理视图及其子视图. UIViewController的生命周期: 运行APP--载入视图loadView--调用viewDidLoad方法--调用viewWillAppear方法--调用viewDidAppear方法--正

iOS 在视图控制器里面判断 应用程序的前台 后台切换 UIViewController

1.时机  用户点击home 键  应用退到后台 再次点击进入前台  在UIViewController里面 控制器如何获取相关的事件? 2.需求 (1)NSTimer   在应用程序进入后台 10秒至10分钟会停止(10分钟左右一般应用也会被kill)  但是只要在后台这个nstimer 运行 就是邋遢的 所以我想停止它 在应用进入后台的时候 (2)程序支持后台下载,当用户停止在下载管理界面时,然后用户点击home键使程序进入后台运行,等用户返回到程序时,下载管理界面需要显示最新的下载进度.(

003-多视图控制器切换

多视图控制器切换 1.容器的概念 • 一个iOS的app很少只由一个ViewController组成,除非这个app极其简单.当app中有多个ViewController时,就需要对这些ViewController进行管理 • 容器的概念:负责展示一个或者多个ViewController,并管理这些 ViewController的生命周期 • ?部分容器本身也是一个ViewController,被容器管理的控制器称为容器的?控制器(childViewController),?容器被称为父控制器(

iOS之浅谈纯代码控制UIViewController视图控制器跳转界面的几种方法

一.最普通的视图控制器UIViewContoller 一个普通的视图控制器一般只有模态跳转的功能(ipad我不了解除外,这里只说iPhone),这个方法是所有视图控制器对象都可以用的,而实现这种功能,有两种方法. 1.通过方法 - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion跳转 相

iOS 7:用代码解决视图控制器的View整体上移问题

如果你准备将你的老的 iOS 6 app 迁移到 iOS 7 上,那么你必须注意了.当你的老的 app 在 iOS 7 设备上运行时,所有ViewController 的视图都整体上移了,因为 iOS 7 把整个屏幕高度(包括状态栏和导航栏)都作为了视图控制器的有效高度.于是你的视图上移了,并和上层的状态栏交叠在一起. 你当然可以在 Xcode 中修改每个 View,将他们下移20个像素(状态栏高度)或者64个像素(状态栏+导航栏高度). 但是苹果显然已经考虑到这个问题,他们在 iOS 7 SD

IOS视图控制器的生命周期

原创Blog,转载请注明出处 http://blog.csdn.net/hello_hwc?viewmode=contents 所谓的生命周期,也就是几个函数的调用顺序,这里以用Storyboard来创建一个ViewController为例 然后我们测试如下代码 // // ViewController.m // // Created by huangwenchen on 14/12/26. // Copyright (c) 2014年 huangwenchen. All rights rese

[转]iOS开发之视图控制器(UIViewController)

视图控制器应该在MVC设计模式中扮演控制层(C)的角色,UIViewController的职责对内管理与之关联的View,对外跟其他UIViewController通信和协调.一个视图控制器管理一个视图(它可以有子视图),其view属性指向它所管理的视图.UIViewController类可以有子类,可以使用一个系统的UIViewController子类或者直接自己创建一个UIViewController的子类. 使用代码创建控制器和视图. 开始创建一个基于窗口的Empty Applicatio