iOS开发UI篇—Modal简单介绍
iOS开发UI篇—Modal简单介绍
一、简单介绍
除了push之外,还有另外一种控制器的切换方式,那就是Modal
任何控制器都能通过Modal的形式展?出来
Modal的默认效果:新控制器从屏幕的最底部往上钻,直到盖住之前的控制器为?
二、代码说明
新建一个项目,在Application的代理中添加window和控制器。
TXAppDelegate.m文件
1 #import "TXAppDelegate.h" 2 #import "TXOneViewController.h" 3 4 @implementation TXAppDelegate 5 6 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 7 { 8 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 9 10 self.window.rootViewController = [[TXOneViewController alloc] init]; 11 12 [self.window makeKeyAndVisible]; 13 return YES; 14 } 15
打开modal窗口
TXViewController.m文件
1 #import "TXOneViewController.h" 2 #import "TXTwoViewController.h" 3 4 @interface TXOneViewController () 5 - (IBAction)jump; 6 7 @end 8 9 @implementation TXOneViewController 10 11 - (void)viewDidLoad 12 { 13 [super viewDidLoad]; 14 // Do any additional setup after loading the view from its nib. 15 } 16 17 - (IBAction)jump { 18 // 展示TXTwoViewController 19 TXTwoViewController *two = [[TXTwoViewController alloc] init]; 20 21 UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:two]; 22 23 [self presentViewController:nav animated:YES completion:^{ 24 NSLog(@"展示TXTwoViewController完毕......."); 25 }]; 26 } 27 @end
移除modal视图
TXtwoViewController.m文件
1 #import "TXTwoViewController.h" 2 3 @interface TXTwoViewController () 4 - (IBAction)cancel; 5 6 @end 7 8 @implementation TXTwoViewController 9 10 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 11 { 12 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 13 if (self) { 14 // Custom initialization 15 } 16 return self; 17 } 18 19 - (void)viewDidLoad 20 { 21 [super viewDidLoad]; 22 23 self.title = @"第2个控制器"; 24 // self.navigationItem.title 25 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)]; 26 } 27 28 - (void)didReceiveMemoryWarning 29 { 30 [super didReceiveMemoryWarning]; 31 // Dispose of any resources that can be recreated. 32 } 33 34 - (IBAction)cancel { 35 // NSLog(@"%@", self.view.window.rootViewController); 36 // NSLog(@"%@", self.view.window.subviews); 37 [self dismissViewControllerAnimated:YES completion:^{ 38 NSLog(@"关闭TXTwoViewController...."); 39 }]; 40 // [self.navigationController dismissViewControllerAnimated:YES completion:^{ 41 // NSLog(@"关闭TXTwoViewController...."); 42 // }]; 43 } 44 @end
三、注意点
1 //创建一个新的modal并弹出 2 TXtwoViewController *two=[[TXtwoViewController alloc]init]; 3 //在two上用导航控制器包装,让弹出的模态窗口有一个导航栏可以放返回按钮 4 UINavigationController *nvc=[[UINavigationController alloc]initWithRootViewController:two 5 ]; 6 [self presentViewController:nvc animated:YES completion:^{ 7 NSLog(@"弹出一个模态窗口"); 8 }];
1 //编写点击返回按钮的点击事件 2 //点击返回按钮,移除当前模态窗口 3 // [self.navigationController dismissViewControllerAnimated:YES completion:^{ 4 // NSLog(@"移除模态窗口"); 5 // }]; 6 7 // 如果一个控制器是以模态的形式展现出来的, 可以调用该控制器以及该控制器的子控制器让让控制器消失 8 [self dismissViewControllerAnimated:YES completion:^{ 9 NSLog(@"移除"); 10 }];
五、数据的传递
项目文件结构和storyboard
代码示例:
TXViewController.m文件
1 #import "TXViewController.h" 2 #import "TXTwoViewController.h" 3 4 @interface TXViewController () 5 6 @end 7 8 @implementation TXViewController 9 10 - (void)viewDidLoad 11 { 12 [super viewDidLoad]; 13 // Do any additional setup after loading the view, typically from a nib. 14 } 15 /* 16 如果控制器之间的关系比较紧密一般用 UINavigationController 17 如果控制器之间的关系不是很紧密可以用Modal 18 */ 19 - (void)didReceiveMemoryWarning 20 { 21 [super didReceiveMemoryWarning]; 22 // Dispose of any resources that can be recreated. 23 } 24 //通过segue跳转前,会调用这个方法,在这个方法中把数据传递给弹出来的模态窗口 25 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 26 { 27 UINavigationController *nav = segue.destinationViewController; 28 29 TXTwoViewController *two = (TXTwoViewController *)nav.topViewController; 30 31 two.name = @"小说哥"; 32 33 } 34 35 @end
TXtwoViewController.h文件
1 #import <UIKit/UIKit.h> 2 3 @interface TXTwoViewController : UIViewController 4 @property (nonatomic, copy) NSString *name; 5 @end
TXtwoViewController.m文件
#import "TXTwoViewController.h" @interface TXTwoViewController () - (IBAction)cancel:(id)sender; @end @implementation TXTwoViewController - (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. NSLog(@"%@", self.name); } - (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. } */ - (IBAction)cancel:(id)sender { [self dismissViewControllerAnimated:YES completion:nil]; } @end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。