iOS開發從入門到精通--UIViewController使用

來源:互聯網
上載者:User

標籤:

UIViewController使用:從ViewController.m跳轉到ViewController02.m,ViewController.m的生命週期問題,ViewController02消失之後再顯示ViewController的過程
ViewController.m裡面的代碼:

#import "ViewController.h"#import "ViewController02.h"@interface ViewController ()@end@implementation ViewController//當螢幕被點擊時,調用此函數- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    //建立視圖控制器2    ViewController02 *vc =[[ViewController02 alloc]init];    //顯示一個新的視圖控制器到螢幕上    //p1:新的視圖控制器對象    //p2:使用動畫切換動畫效果    //p3:切換結束後功能調用,不需要直接傳nil    [self presentViewController:vc animated:YES completion:nil];}//第一次程式載入視圖時調用- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    self.view.backgroundColor=[UIColor blueColor];    NSLog(@"viewDidLoad 第一次載入視圖");}//當視圖控制器的視圖即將顯示時,調用此函數//視圖分為:1顯示前(不顯示)2正在處於顯示狀態 3已經被隱藏//參數:表示是否用動畫切換後顯示//每一次視圖顯示時都會被調用- (void) viewWillAppear:(BOOL)animated{    NSLog(@"viewWillAppear 視圖即將顯示");}//視圖即將消失時,調用此函數//參數:表示是否調用動畫切換後消失//目前狀態:視圖還是顯示在螢幕上的- (void) viewWillDisappear:(BOOL)animated{    NSLog(@"viewDidDisappear 視圖即將消失");}//當視圖已經顯示到螢幕後的瞬間調用此函數//參數:表示是否調用動畫切換顯示//目前狀態:已經顯示到螢幕上- (void) viewDidAppear:(BOOL)animated{    NSLog(@"viewDidAppear 視圖已經顯示");}//當前視圖已經消失從螢幕上//參數:表示是否調用動畫//目前狀態:當前視圖控制視圖已經消失從螢幕上- (void) viewDidDisappear:(BOOL)animated{    NSLog(@"viewDidDisappear 視圖已經消失");}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

建立一個ViewController02,在ViewController02.m裡代碼如下:

#import "ViewController02.h"@interface ViewController02 ()@end@implementation ViewController02- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    //控制器2的顏色為紅色    self.view.backgroundColor=[UIColor redColor];}//點擊當前控制器2的介面螢幕時- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    //使當前的控制器消失掉    //p1:是否有動畫效果    //p2:結束後是否調用功能block塊操作    [self dismissViewControllerAnimated:YES completion:nil];}- (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

iOS開發從入門到精通--UIViewController使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.