IOS开发:模态视图的操作小结(一)


什么是模态视图呢?简单的说模态视图就是:如果你不关闭当前已经打开的视图窗口,就不能进行其他操作。模态视图使用起来非常简单,需要记住两个方法:

1、显示模态视图 presentViewController:animated:completion ;

2、关闭模态视图 dismissViewControllerAnimated:completion;

下面来做个简单的案例:

一、显示模态视图

首先我们创建一个新的工程,在跟视图上添加一个Button->modelButton按钮


[modelButton addTarget:self action:@selector(presentModel) forControlEvents:UIControlEventTouchUpInside];

- (void)presentModel {

    ModelViewController *modelVC = [[ModelViewController alloc] init];

    modeVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;//设置视图弹出时的动画效果

    [self presentViewController:ModelVC animated:YES completion:^{

       

    }];

}

二、模态的关闭

在创建的模态视图中添加个一个Button->colseButton按钮

[closeButton addTarget:self action:@selector(closeModel) forControlEvents:UIControlEventTouchUpInside];

- (void)closeModel{

    [self dismissViewControllerAnimated:YES completion:^{

       

    }];

}


  • 模态视图出现的场景一般是临时弹出的窗口,譬如:登陆窗口;

  • 模态视图弹出时通过modalTransitionStyle属性设置不同的动画效果;

  • 调用dismissModalViewControllerAnimated: 方法关闭窗口




根据以上内容,再给大家做个简单案例:

1、新建一个工程,选单视图应用模版

28183922-6080583cbe2b4c0cb5c67ea77ca0afee.png

2、添加一个button用于在当前视图上弹出一个模态视图

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    //添加弹出模态视图按钮
    UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:CGRectMake(120, 220, 80, 40)];
    [button setTitle:@"模态视图" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

-(void)buttonPressed
{
    
}

3、创建一个新建的视图控制器,用来展示点击按钮后要弹出的视图

28185026-495a73ddcd5349edb0c530e030a77b61.png

{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor purpleColor];
    UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:CGRectMake(130, 50, 60, 20)];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back ) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

}

-(void)back
{
    //[self dismissModalViewControllerAnimated:YES];6.0 不能用

//下面这行代码作用就是将弹出的模态视图移除,第一个yes表示移除的时候有动画效果,第二参数是设置一个回调,当模态视图移除消失后,会回到这里,可以在这里随便写句话打个断点,试一下就知道确实会回调到这个方法

    [selfdismissViewControllerAnimated:YEScompletion:^{

        NSLog(@"back");//这里打个断点,点击按钮模态视图移除后会回到这里
 //ios 5.0以上可以用该方法

    }];


}


现在给主视图:ViewController.m中“按钮添加”----》“弹出模态视图”方法:

-(void)buttonPressed
{
    ModalViewController * modalView = [[ModalViewController alloc]init];
    modalView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    
   // [self presentModalViewController:modalView animated:YES];  ios 6 弃用了该方法
    [self presentViewController:modalView animated:YES completion:nil];
    [modalView release];
}


最后运行效果如下:

28191627-c6b7d0e9655c4bff850d9997457e6ab0.png28191718-fcb2cbccebad4cd2b377206f75ef1aad.png


我们设置一个触发关闭方法的button -> dismissButton

程序默认的动画效果是从下往上弹出,可以改modalTransitionStyle换

成其他效果


    modalView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

typedef NS_ENUM(NSInteger, UIModalTransitionStyle) {


    UIModalTransitionStyleCoverVertical = 0,//默认垂直向上

    UIModalTransitionStyleFlipHorizontal,// 翻转效果

    UIModalTransitionStyleCrossDissolve,//淡入淡出

    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2

      UIModalTransitionStylePartialCurl,//翻页效果

    #endif

};

这章先到这里,下章继续小结视图之间的切换和传值方法。


注:本文转载自拾图网[http://www.tshinet.com],转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除。
上一篇 下一篇

分享

最新评论

最新加入


5.1869s