//視圖控制器:UIViewController //功能:1:管理一個界面中的全部子視圖 //2:管理全部的當前視圖的(消息)事件函數 //3:管理界面中的全部數據(源),使數據和界面分離管理 //4:負責網絡數據處理功能 #import "VCRoot.h" #import "VCSecond.h" @interface VCRoot () @end @implementation VCRoot - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } //當視圖第一次「顯示」的時候 //當視圖被建立時,此函數被調用 //只能調用一次,至關於初始化函數 - (void)viewDidLoad { //self.view ; [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor grayColor] ; UIButton* btn =[UIButton buttonWithType:UIButtonTypeRoundedRect] ; btn.frame = CGRectMake(100, 100, 120, 40) ; btn.titleLabel.font = [UIFont systemFontOfSize:24] ; [btn setTitle:@"切換界面" forState:UIControlStateNormal] ; //設置標題顏色 [btn setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal] ; [btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside] ; //將子視圖添加到界面上 [self.view addSubview:btn] ; //建立控制器二 _vcSecond = [[VCSecond alloc] init] ; //設置動畫風格 //控制器一切換到控制器二時的動畫 //UIModalTransitionStyleCoverVertical //自下向上切換風格 //UIModalTransitionStyleCrossDissolve //漸變風格 //UIModalTransitionStyleFlipHorizontal //翻轉風格 //UIModalTransitionStylePartialCurl //部分翻頁風格 _vcSecond.modalTransitionStyle = UIModalTransitionStyleCoverVertical ; } -(void) viewWillAppear:(BOOL)animated { NSLog(@"視圖1即將顯示!"); } -(void) viewWillDisappear:(BOOL)animated { NSLog(@"視圖1即將消失!"); } -(void) viewDidAppear:(BOOL)animated { NSLog(@"視圖1已經顯示!"); } -(void) viewDidDisappear:(BOOL)animated { NSLog(@"視圖1已經消失!"); } -(void) pressBtn { //VCSecond* _vcSecond = [[VCSecond alloc] init] ; //功能:從一個視圖控制器切換到另一個視圖控制器 //self:表示當前的視圖控制器VCRoot //參數一:vcSecond表示即將切換到的視圖控制器 //參數二:是有使用動畫效果 //參數三:當切換動畫結束後,調用的代碼功能 [self presentViewController:_vcSecond animated:YES completion:nil] ; // [_vcSecond release] ; } -(void) dealloc { [_vcSecond release] ; [super dealloc] ; } #import "VCSecond.h" @interface VCSecond () @end @implementation VCSecond - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //更改背景顏色 self.view.backgroundColor = [UIColor redColor] ; NSLog(@"視圖二被建立!"); } //四個函數爲視圖控制器的顯示消失函數 //每當視圖控制器被顯示和消失時,都會被調用 -(void) viewWillAppear:(BOOL)animated { NSLog(@"視圖二即將顯示!"); } -(void) viewWillDisappear:(BOOL)animated { NSLog(@"-------------------------"); NSLog(@"視圖二即將消失!"); } -(void) viewDidAppear:(BOOL)animated { NSLog(@"視圖二已經顯示!"); NSLog(@"------------------------"); } -(void) viewDidDisappear:(BOOL)animated { NSLog(@"視圖已經消失!"); } //點擊屏幕空白處 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //將視圖控制器對象消失掉 //內存是否銷燬,取決因而否還有其餘對象引用 //若是還有引用,不能銷燬 //若有沒有對象引用,消失後會銷燬內存 //參數一:是否有動畫 //參數二:消失後的功能代碼調用 [self dismissViewControllerAnimated:YES completion:nil] ; }