iOS开发UI篇—Modal简单介绍
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 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

打开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 @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

移除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 @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

三、注意点

//创建一个新的modal并弹出
YYtwoViewController *two=[[YYtwoViewController alloc]init];
//在two上用导航控制器包装,让弹出的模态窗口有一个导航栏可以放返回按钮
UINavigationController *nvc=[[UINavigationController alloc]initWithRootViewController:two
];
[self presentViewController:nvc animated:YES completion:^{
NSLog(@"弹出一个模态窗口");
}];


//编写点击返回按钮的点击事件
//点击返回按钮,移除当前模态窗口
// [self.navigationController dismissViewControllerAnimated:YES completion:^{
// NSLog(@"移除模态窗口");
// }]; // 如果一个控制器是以模态的形式展现出来的, 可以调用该控制器以及该控制器的子控制器让让控制器消失
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"移除");
}];

五、数据的传递
项目文件结构和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 @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

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 : UIViewController
12 @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 @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简单介绍的更多相关文章
- iOS开发UI篇—UITabBarController简单介绍
iOS开发UI篇—UITabBarController简单介绍 一.简单介绍 UITabBarController和UINavigationController类似,UITabBarControlle ...
- iOS开发UI篇—Kvc简单介绍
ios开发UI篇—Kvc简单介绍 一.KVC简单介绍 KVC key valued coding 键值编码 KVC通过键值间接编码 补充: 与KVC相对的时KVO,即key valued observ ...
- iOS开发UI篇—UIWindow简单介绍
iOS开发UI篇—UIWindow简单介绍 一.简单介绍 UIWindow是一种特殊的UIView,通常在一个app中只会有一个UIWindow iOS程序启动完毕后,创建的第一个视图控件就是UIWi ...
- iOS开发UI篇—Quartz2D简单介绍
iOS开发UI篇—Quartz2D简单介绍 一.什么是Quartz2D Quartz 2D是⼀个二维绘图引擎,同时支持iOS和Mac系统 Quartz 2D能完成的工作: 绘制图形 : 线条\三角形\ ...
- iOS开发UI篇—popoverController简单介绍
iOS开发UI篇—popoverController简单介绍 一.简单介绍 1.什么是UIPopoverController 是iPad开发中常见的一种控制器(在iPhone上不允许使用) 跟其他控制 ...
- iOS开发UI篇—popoverController简单介绍(ipad)
一.简单介绍 1.什么是UIPopoverController 是iPad开发中常见的一种控制器(在iPhone上不允许使用) 跟其他控制器不一样的是,它直接继承自NSObject,并非继承自UIVi ...
- 文顶顶 iOS开发UI篇—UITabBarController简单介绍 iOS开发UI篇—UITabBarController简单介绍
一.简单介绍 UITabBarController和UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型的例 ...
- iOS开发多线程篇—多线程简单介绍
iOS开发多线程篇—多线程简单介绍 一.进程和线程 1.什么是进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开QQ.Xcod ...
- iOS开发拓展篇—UIDynamic(简单介绍)
iOS开发拓展篇—UIDynamic(简单介绍) 一.简单介绍 1.什么是UIDynamic UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架 可以认为是一种物理引擎,能模拟 ...
随机推荐
- [转]七天学会NodeJS
转:http://nqdeng.github.io/7-days-nodejs/ NodeJS基础 什么是NodeJS JS是脚本语言,脚本语言都需要一个解析器才能运行.对于写在HTML页面里的JS, ...
- 洛谷 P1803 凌乱的yyy Label:Water 贪心
题目背景 快noip了,yyy很紧张! 题目描述 现在各大oj上有n个比赛,每个比赛的开始.结束的时间点是知道的. yyy认为,参加越多的比赛,noip就能考的越好(假的) 所以,他想知道他最多能参加 ...
- 【CF】148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题意:w个白b个黑,公主和龙轮流取,公主先取,等概率取到一个.当龙取完后,会等概率跳出一只.(0<= ...
- css比较容易搞混的三个选择器
直接后代选择器:> .grid>input[type="button"] 所有后代选择器:空格 .grid input[type="button"] ...
- C#_生成HTML
#region 生成静态页 /// <summary> /// 生成静态页 /// </summary> /// <param name="URL"& ...
- FPGA的典型应用领域
本文关键字:fpga应用,fpga应用领域, fpga培训,FPGA应用开发入门与典型实例 一.数据采集和接口逻辑领域 1.FPGA在数据采集领域的应用 由于自然界的信号大部分是模拟信号,因此一般的信 ...
- ios修改产品名
在创建项目的时候,会设置一个项目名,以后生成的APP名字也就是这个了,但由于某种原因,我想修改APP名字,也就是屏幕程序图标下面显示的那个,这该怎么办呢? 下面有三种方法都可以: 修改Product ...
- Oracle10g RAC的简单操作
1.查看OCR位置用户指定的位置会被放置在 /etc/oracle/ocr.loc(Liunx系统) 或 /var/opt/oracle/ocr.loc [oracle@rac4 opt]$ cat ...
- MVC概念性的内容
MVC: 是一个缩写(model + view + control), Model:是一些类文件, 功能:负责增删改查, 负责跟数据库打交道 (把数据存入到数据库: 从数据库把数据读 ...
- window svn backup.bat
help command /? call /? %cd% 可以用在批处理文件中,也可以用在命令行中:展开后,是驱动器盘符:+当前目录,如在dos窗口中进入c:\dir目录下面, %0代指批处理文件自身 ...