iOS开发UI篇—Modal简单介绍

时间:2022-03-08 14:21:02

iOS开发UI篇—Modal简单介绍

一、简单介绍

除了push之外,还有另外一种控制器的切换方式,那就是Modal

任何控制器都能通过Modal的形式展⽰出来

Modal的默认效果:新控制器从屏幕的最底部往上钻,直到盖住之前的控制器为⽌

二、代码说明

新建一个项目,在Application的代理中添加window和控制器。

YYAppDelegate.m文件

iOS开发UI篇—Modal简单介绍
 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 YYAppDelegate
13
14 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
15 {
16 //1.创建window,并设置window的frame
17 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
iOS开发UI篇—Modal简单介绍

打开modal窗口

YYViewController.m文件

iOS开发UI篇—Modal简单介绍
 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 @end
17
18 @implementation YYViewController
19
20 - (void)viewDidLoad
21 {
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:two
32 ];
33 [self presentViewController:nvc animated:YES completion:^{
34 NSLog(@"弹出一个模态窗口");
35 }];
36
37 }
38 @end
iOS开发UI篇—Modal简单介绍

移除modal视图

YYtwoViewController.m文件

iOS开发UI篇—Modal简单介绍
 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 @end
14
15 @implementation YYtwoViewController
16
17 - (void)viewDidLoad
18 {
19 [super viewDidLoad];
20
21 //给导航条添加一个返回按钮
22 self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(change)];
23 }
24
25 -(void)change
26 {
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
iOS开发UI篇—Modal简单介绍

三、注意点

(1)modal的特点:当modal窗口弹出(从下往上)的时候,后面的视图不可点 
(2)弹出控制器的视图(通过这种方式只能弹出一个视图)
iOS开发UI篇—Modal简单介绍
   //创建一个新的modal并弹出
YYtwoViewController *two=[[YYtwoViewController alloc]init];
//在two上用导航控制器包装,让弹出的模态窗口有一个导航栏可以放返回按钮
UINavigationController *nvc=[[UINavigationController alloc]initWithRootViewController:two
];
[self presentViewController:nvc animated:YES completion:^{
NSLog(@"弹出一个模态窗口");
}];
iOS开发UI篇—Modal简单介绍
(3)移除控制器的视图(两种方式都可以)
iOS开发UI篇—Modal简单介绍
    //编写点击返回按钮的点击事件
//点击返回按钮,移除当前模态窗口
// [self.navigationController dismissViewControllerAnimated:YES completion:^{
// NSLog(@"移除模态窗口");
// }]; // 如果一个控制器是以模态的形式展现出来的, 可以调用该控制器以及该控制器的子控制器让让控制器消失
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"移除");
}];
iOS开发UI篇—Modal简单介绍
(4)提示在实际的开发中,如果控制器之间的关系紧密一般用导航控制器,如果控制器之间的关系不是很紧密就用modal
四、内部机制
(1)弹出之后,window上面只有一个子视图。
(2)虽然当前界面上展示在我们眼前的时twoview,但是window的根控制器仍然是NJviewController,它并没有切换window的根控制器,而仅仅只是换了window上面显示的视图。
(3)移除的视图并没有销毁,因为控制器并没有销毁,所以控制器对应的view也没有销毁。
(4)在模态弹出(完全显示后),在方法中传入two作为参数,默认就有一个控制器强引用着它。
(5)当向下移除之后,只要调用了控制器的dismiss方法让窗口关闭,modal就释放了。
(6)通常弹出的模态窗口都会提供一个导航条,让界面拥有导航条的最快的方式是给它包装一个导航控制器。
(7)如果一个控制器是以模态的形式展现出来的。可以调用该控制器以及该控制器的子控制器,让该控制器消失。

五、数据的传递

项目文件结构和storyboard

iOS开发UI篇—Modal简单介绍

代码示例:

YYViewController.m文件

iOS开发UI篇—Modal简单介绍
 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 @end
15
16 @implementation YYViewController
17
18 - (void)viewDidLoad
19 {
20 [super viewDidLoad];
21 }
22
23 - (void)didReceiveMemoryWarning
24 {
25 [super didReceiveMemoryWarning];
26 }
27
28
29 /*
30 如果控制器之间的关系比较紧密一般用 UINavigationController
31 如果控制器之间的关系不是很紧密可以用Modal
32 */
33
34 //通过segue跳转前,会调用这个方法,在这个方法中把数据传递给弹出来的模态窗口
35 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
36 {
37 //拿到目标控制器
38 UINavigationController *nav=segue.destinationViewController;
39 YYtwoViewController *two=(YYtwoViewController *)nav.topViewController;
40 //传递数据
41 two.name=@"文顶顶";
42 }
43 @end
iOS开发UI篇—Modal简单介绍

YYtwoViewController.h文件

iOS开发UI篇—Modal简单介绍
 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 : UIViewController
12 @property(nonatomic,copy)NSString *name;
13 @end
iOS开发UI篇—Modal简单介绍

YYtwoViewController.m文件

iOS开发UI篇—Modal简单介绍
 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 @end
15
16 @implementation YYtwoViewController
17
18
19 - (void)viewDidLoad
20 {
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)black
29 {
30 //移除模态窗口
31 [self dismissViewControllerAnimated:YES completion:^{
32 NSLog(@"成功移除!");
33 }];
34 }
35 @end
iOS开发UI篇—Modal简单介绍