iOS開發UI篇—Modal簡單介紹

來源:互聯網
上載者:User

標籤:

iOS開發UI篇—Modal簡單介紹

 

一、簡單介紹

除了push之外,還有另外一種控制器的切換方式,那就是Modal

任何控制器都能通過Modal的形式展?出來

Modal的預設效果:新控制器從螢幕的最底部往上鑽,直到蓋住之前的控制器為?

二、代碼說明

建立一個項目,在Application的代理中添加window和控制器。

YYAppDelegate.m檔案

 1 // 2 //  YYAppDelegate.m 3 //  01-modal 4 // 5 //  Created by apple on 14-6-9. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import "YYAppDelegate.h"10 #import "YYViewController.h"11 12 @implementation YYAppDelegate13 14 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions15 {16     //1.建立window,並設定window的frame17     self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];18     //2.設定window的背景顏色為黑色19     self.window.backgroundColor=[UIColor blackColor];20 21     22     //建立一個導航控制器作為子控制器23     YYViewController *one=[[YYViewController alloc]init];24     self.window.rootViewController=one;25     26     //3.設定window為主視窗,並顯示27     [self.window makeKeyAndVisible];28     return YES;29 }30 31 32 @end

開啟modal視窗

YYViewController.m檔案

 1 // 2 //  YYViewController.m 3 //  01-modal 4 // 5 //  Created by apple on 14-6-9. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import "YYViewController.h"10 #import "YYtwoViewController.h"11 12 @interface YYViewController ()13 //當點擊的時候,跳轉到第二個介面14 - (IBAction)jump2two:(UIButton *)sender;15 16 @end17 18 @implementation YYViewController19 20 - (void)viewDidLoad21 {22     [super viewDidLoad];23     // Do any additional setup after loading the view from its nib.24 }25 26 27 - (IBAction)jump2two:(UIButton *)sender {28     //建立一個新的modal並彈出29     YYtwoViewController *two=[[YYtwoViewController alloc]init];30     //在two上用導航控制器封裝,讓彈出的模態視窗有一個導覽列可以放返回按鈕31     UINavigationController *nvc=[[UINavigationController alloc]initWithRootViewController:two32                                  ];33     [self presentViewController:nvc animated:YES completion:^{34         NSLog(@"彈出一個模態視窗");35     }];36     37 }38 @end

移除modal視圖

YYtwoViewController.m檔案

 1 // 2 //  YYtwoViewController.m 3 //  01-modal 4 // 5 //  Created by apple on 14-6-9. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import "YYtwoViewController.h"10 11 @interface YYtwoViewController ()12 13 @end14 15 @implementation YYtwoViewController16 17 - (void)viewDidLoad18 {19     [super viewDidLoad];20 21     //給導航條添加一個返回按鈕22     self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(change)];23 }24 25 -(void)change26 {27     //編寫點擊返回按鈕的點擊事件28     //點擊返回按鈕,移除當前模態視窗29 //    [self.navigationController dismissViewControllerAnimated:YES completion:^{30 //        NSLog(@"移除模態視窗");31 //    }];32     33 // 如果一個控制器是以模態的形式展現出來的, 可以調用該控制器以及該控制器的子控制器讓讓控制器消失34     [self dismissViewControllerAnimated:YES completion:^{35         NSLog(@"移除");36     }];37 }38 39 @end

三、注意點

(1)modal的特點:當modal視窗彈出(從下往上)的時候,後面的視圖不可點 (2)彈出控制器的視圖(通過這種方式只能彈出一個視圖)
   //建立一個新的modal並彈出    YYtwoViewController *two=[[YYtwoViewController alloc]init];    //在two上用導航控制器封裝,讓彈出的模態視窗有一個導覽列可以放返回按鈕    UINavigationController *nvc=[[UINavigationController alloc]initWithRootViewController:two                                 ];    [self presentViewController:nvc animated:YES completion:^{        NSLog(@"彈出一個模態視窗");    }];
(3)移除控制器的視圖(兩種方式都可以)
    //編寫點擊返回按鈕的點擊事件    //點擊返回按鈕,移除當前模態視窗//    [self.navigationController dismissViewControllerAnimated:YES completion:^{//        NSLog(@"移除模態視窗");//    }];    // 如果一個控制器是以模態的形式展現出來的, 可以調用該控制器以及該控制器的子控制器讓讓控制器消失    [self dismissViewControllerAnimated:YES completion:^{        NSLog(@"移除");    }];
(4)提示在實際的開發中,如果控制器之間的關係緊密一般用導航控制器,如果控制器之間的關係不是很緊密就用modal 四、內部機制(1)彈出之後,window上面只有一個子視圖。(2)雖然當前介面上展示在我們眼前的時twoview,但是window的根控制器仍然是NJviewController,它並沒有切換window的根控制器,而僅僅只是換了window上面顯示的視圖。(3)移除的視圖並沒有銷毀,因為控制器並沒有銷毀,所以控制器對應的view也沒有銷毀。(4)在模態彈出(完全顯示後),在方法中傳入two作為參數,預設就有一個控制器強引用著它。(5)當向下移除之後,只要調用了控制器的dismiss方法讓視窗關閉,modal就釋放了。(6)通常彈出的模態視窗都會提供一個導航條,讓介面擁有導航條的最快的方式是給它封裝一個導航控制器。(7)如果一個控制器是以模態的形式展現出來的。可以調用該控制器以及該控制器的子控制器,讓該控制器消失。 

五、資料的傳遞

專案檔結構和storyboard

程式碼範例:

YYViewController.m檔案

 1 // 2 //  YYViewController.m 3 //  02-模態視窗的資料傳遞 4 // 5 //  Created by apple on 14-6-9. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import "YYViewController.h"10 #import "YYtwoViewController.h"11 12 @interface YYViewController ()13 14 @end15 16 @implementation YYViewController17 18 - (void)viewDidLoad19 {20     [super viewDidLoad];21 }22 23 - (void)didReceiveMemoryWarning24 {25     [super didReceiveMemoryWarning];26 }27 28 29 /*30  如果控制器之間的關係比較緊密一般用 UINavigationController31  如果控制器之間的關係不是很緊密可以用Modal32  */33 34 //通過segue跳轉前,會調用這個方法,在這個方法中把資料傳遞給彈出來的模態視窗35 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender36 {37     //拿到目標控制器38     UINavigationController *nav=segue.destinationViewController;39     YYtwoViewController *two=(YYtwoViewController *)nav.topViewController;40     //傳遞資料41     [email protected]"文頂頂";42 }43 @end

YYtwoViewController.h檔案

 1 // 2 //  YYtwoViewController.h 3 //  02-模態視窗的資料傳遞 4 // 5 //  Created by apple on 14-6-9. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import <UIKit/UIKit.h>10 11 @interface YYtwoViewController : UIViewController12 @property(nonatomic,copy)NSString *name;13 @end

YYtwoViewController.m檔案

 1 // 2 //  YYtwoViewController.m 3 //  02-模態視窗的資料傳遞 4 // 5 //  Created by apple on 14-6-9. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import "YYtwoViewController.h"10 11 @interface YYtwoViewController ()12 @property (weak, nonatomic) IBOutlet UILabel *nametext;13 14 @end15 16 @implementation YYtwoViewController17 18 19 - (void)viewDidLoad20 {21     [super viewDidLoad];22     self.nametext.text=self.name;23     24     //為導覽列添加一個返回按鈕25     self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(black)];26 }27 28 -(void)black29 {30     //移除模態視窗31     [self dismissViewControllerAnimated:YES completion:^{32         NSLog(@"成功移除!");33     }];34 }35 @end

 

 轉自:http://www.cnblogs.com/wendingding/p/3778853.html  

iOS開發UI篇—Modal簡單介紹

聯繫我們

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