iOS UI03_UIViewController視圖控制器

來源:互聯網
上載者:User

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

@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



 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.