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

    五、数据的传递

    项目文件结构和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
  • 相关阅读:
    Flask之threading.loacl方法
    websocket之简易聊天室
    websocket介绍
    Flask之jinja2模板语言
    Flask之基础
    Linux之项目的部署
    Linux之nginx负载均衡
    Linux之nginx
    Linux之redis-cluster
    Linux之redis-sentinel
  • 原文地址:https://www.cnblogs.com/asd5551680/p/4070471.html
Copyright © 2020-2023  润新知