ios-使用模态视图

// 模态视图关键的两个方法:

// presentViewController: animated: completion: 呈现模态视图

// dismissViewControllerAnimated: completion: 关闭模态视图

iPhone模态视图,代码实现如下:

 

  1.  
    - (void)dealloc
  2.  
    {
  3.  
     
  4.  
    [self.textField release];
  5.  
    [super dealloc];
  6.  
     
  7.  
    // 取消观察者
  8.  
    [[NSNotificationCenter defaultCenter] removeObserver:self];
  9.  
    }
  10.  
     
  11.  
    - (void)viewDidLoad
  12.  
    {
  13.  
    [super viewDidLoad];
  14.  
    self.view.backgroundColor = [UIColor grayColor];
  15.  
     
  16.  
    // 注册观察者
  17.  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handle:) name:@"registerBroadcastMessage" object:nil];
  18.  
     
  19.  
    [self initUI];
  20.  
    }
  21.  
     
  22.  
    - (void)initUI
  23.  
    {
  24.  
    UILabel *label = [[UILabel alloc]init];
  25.  
    label.frame = CGRectMake(20, 50, 70, 30);
  26.  
    label.backgroundColor = [UIColor clearColor];
  27.  
    label.text = @"用户ID:";
  28.  
    [self.view addSubview:label];
  29.  
    [label release];
  30.  
     
  31.  
    UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(100, 50, 200, 30)];
  32.  
    txtField.backgroundColor = [UIColor whiteColor];
  33.  
    txtField.borderStyle = UITextBorderStyleRoundedRect;
  34.  
    txtField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  35.  
    txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  36.  
    [self.view addSubview:txtField];
  37.  
    [txtField release];
  38.  
     
  39.  
    self.textField = txtField;
  40.  
     
  41.  
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  42.  
    btn.frame = CGRectMake(20, 100, 280, 40);
  43.  
    [btn setTitle:@"注册" forState:UIControlStateNormal];
  44.  
    [btn addTarget:self action:@selector(registerOnClick:) forControlEvents:UIControlEventTouchUpInside];
  45.  
    [self.view addSubview:btn];
  46.  
    }
  47.  
     
  48.  
    - (void)registerOnClick:(id)sender
  49.  
    {
  50.  
    RegisterViewController *registerViewCtl = [[RegisterViewController alloc] init];
  51.  
    registerViewCtl.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  52.  
    [self presentViewController:registerViewCtl animated:YES completion:nil];
  53.  
    [registerViewCtl release];
  54.  
     
  55.  
    // 模态视图关键的两个方法:
  56.  
    // presentViewController: animated: completion: 呈现模态视图
  57.  
    // dismissViewControllerAnimated: completion: 关闭模态视图
  58.  
     
  59.  
    // modalTransitionStyle
  60.  
    /*
  61.  
    UIModalTransitionStyleCoverVertical 呈现时,沿垂直方向由底向上
  62.  
    UIModalTransitionStyleFlipHorizontal 呈现时,水平翻转
  63.  
    UIModalTransitionStyleCrossDissolve 呈现时,两个视图淡入淡出
  64.  
    UIModalTransitionStylePartialCurl 呈现时,模态视图卷起边角
  65.  
    */
  66.  
    }
  67.  
     
  68.  
    - (void)handle:(NSNotification *)notifi
  69.  
    {
  70.  
    NSDictionary *dict = [notifi userInfo];
  71.  
    NSString *str = [dict objectForKey:@"username"];
  72.  
    self.textField.text = str;
  73.  
    }

 


  1.  
    //
  2.  
    // registerViewController.h
  3.  
    //
  4.  
    #import <UIKit/UIKit.h>
  5.  
     
  6.  
    @interface RegisterViewController : UIViewController
  7.  
     
  8.  
    @property (strong, nonatomic) UITextField *textField;
  9.  
     
  10.  
    @end

  1.  
    //
  2.  
    // registerViewController.m
  3.  
    //
  4.  
    #import "RegisterViewController.h"
  5.  
     
  6.  
    @interface RegisterViewController ()
  7.  
     
  8.  
    @end
  9.  
     
  10.  
    @implementation RegisterViewController
  11.  
     
  12.  
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  13.  
    {
  14.  
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  15.  
    if (self)
  16.  
    {
  17.  
     
  18.  
    }
  19.  
    return self;
  20.  
    }
  21.  
     
  22.  
    - (void)dealloc
  23.  
    {
  24.  
    [self.textField release];
  25.  
    [super dealloc];
  26.  
    }
  27.  
     
  28.  
    - (void)viewDidLoad
  29.  
    {
  30.  
    [super viewDidLoad];
  31.  
    self.view.backgroundColor = [UIColor grayColor];
  32.  
     
  33.  
    [self initUI];
  34.  
    }
  35.  
     
  36.  
    - (void)initUI
  37.  
    {
  38.  
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; // ios 7 -> 64
  39.  
    [self.view addSubview:navBar];
  40.  
    [navBar release];
  41.  
     
  42.  
    UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"注册"];
  43.  
    [navBar pushNavigationItem:navItem animated:YES];
  44.  
    [navItem release];
  45.  
     
  46.  
    UIBarButtonItem *barBtnLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneOnClick:)];
  47.  
    navItem.leftBarButtonItem = barBtnLeft;
  48.  
    [barBtnLeft release];
  49.  
     
  50.  
     
  51.  
    UIBarButtonItem *barBtnRight = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveOnClick:)];
  52.  
    navItem.rightBarButtonItem = barBtnRight;
  53.  
    [barBtnRight release];
  54.  
     
  55.  
    UILabel *label = [[UILabel alloc]init];
  56.  
    label.frame = CGRectMake(20, 80, 70, 30);
  57.  
    label.backgroundColor = [UIColor clearColor];
  58.  
    label.text = @"用户ID:";
  59.  
    [self.view addSubview:label];
  60.  
    [label release];
  61.  
     
  62.  
    UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(100, 80, 200, 30)];
  63.  
    txtField.backgroundColor = [UIColor whiteColor];
  64.  
    txtField.borderStyle = UITextBorderStyleRoundedRect;
  65.  
    txtField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  66.  
    txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  67.  
    [self.view addSubview:txtField];
  68.  
    [txtField release];
  69.  
     
  70.  
    self.textField = txtField;
  71.  
    }
  72.  
     
  73.  
    - (void)doneOnClick:(id)sender
  74.  
    {
  75.  
    [self dismissViewControllerAnimated:YES completion:nil];
  76.  
    }
  77.  
     
  78.  
    - (void)saveOnClick:(id)sender
  79.  
    {
  80.  
    NSDictionary *dict = nil;
  81.  
    if (self.textField.text.length)
  82.  
    {
  83.  
    dict = [NSDictionary dictionaryWithObject:self.textField.text forKey:@"username"];
  84.  
    [self dismissViewControllerAnimated:YES completion:nil];
  85.  
    [[NSNotificationCenter defaultCenter] postNotificationName:@"registerBroadcastMessage" object:nil userInfo:dict];
  86.  
    }
  87.  
    else
  88.  
    return;
  89.  
    }
  90.  
     
  91.  
    - (void)didReceiveMemoryWarning
  92.  
    {
  93.  
    [super didReceiveMemoryWarning];
  94.  
    }
  95.  
     
  96.  
    @end

至此,代码实现iPhone模态视图已经介绍完毕,程序运行效果图如下:
      
 
 

iPad模态视图,iPad模态视图和iPad模态视图只是一点点的差异,差异的只是UIModalPresentationFullScreen,代码实现如下:

 

  1.  
    - (void)clickBtn:(id)sender
  2.  
    {
  3.  
    ModalViewController *modalViewCtl = [[ModalViewController alloc] init];
  4.  
    modalViewCtl.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  5.  
     
  6.  
    switch (iSelect)
  7.  
    {
  8.  
    case 0:
  9.  
    modalViewCtl.modalPresentationStyle = UIModalPresentationFullScreen;
  10.  
    break;
  11.  
    case 1:
  12.  
    modalViewCtl.modalPresentationStyle = UIModalPresentationPageSheet;
  13.  
    break;
  14.  
    case 2:
  15.  
    modalViewCtl.modalPresentationStyle = UIModalPresentationFormSheet;
  16.  
    break;
  17.  
    case 3:
  18.  
    modalViewCtl.modalPresentationStyle = UIModalPresentationCurrentContext;
  19.  
    break;
  20.  
    default:
  21.  
    break;
  22.  
    }
  23.  
     
  24.  
    [self presentViewController:modalViewCtl animated:YES completion:nil];
  25.  
    }

至此,代码实现iPad模态视图已经介绍完毕,程序运行效果图如下:

   

文章
posted @ 2018-06-29 08:48  sundaysios  阅读(161)  评论(0编辑  收藏  举报