你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS UI03_UIViewController視圖控制器

iOS UI03_UIViewController視圖控制器

編輯:IOS開發綜合

//

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



 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved