您的位置:首页 > 产品设计 > UI/UE

iOS开发UI篇—Modal简单介绍

2014-11-03 09:39 405 查看
iOS开发UI篇—Modal简单介绍

一、简单介绍

除了push之外,还有另外一种控制器的切换方式,那就是Modal

任何控制器都能通过Modal的形式展⽰出来

Modal的默认效果:新控制器从屏幕的最底部往上钻,直到盖住之前的控制器为⽌

二、代码说明

新建一个项目,在Application的代理中添加window和控制器。

TXAppDelegate.m文件

#import "TXAppDelegate.h"
#import "TXOneViewController.h"

@implementation TXAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

self.window.rootViewController = [[TXOneViewController alloc] init];

[self.window makeKeyAndVisible];
return YES;
}


打开modal窗口

TXViewController.m文件

#import "TXOneViewController.h"
#import "TXTwoViewController.h"

@interface TXOneViewController ()
- (IBAction)jump;

@end

@implementation TXOneViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}

- (IBAction)jump {
// 展示TXTwoViewController
TXTwoViewController *two = [[TXTwoViewController alloc] init];

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:two];

[self presentViewController:nav animated:YES completion:^{
NSLog(@"展示TXTwoViewController完毕.......");
}];
}
@end


移除modal视图

TXtwoViewController.m文件

#import "TXTwoViewController.h"

@interface TXTwoViewController ()
- (IBAction)cancel;

@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];

self.title = @"第2个控制器";
//    self.navigationItem.title
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)cancel {
//    NSLog(@"%@", self.view.window.rootViewController);
//    NSLog(@"%@", self.view.window.subviews);
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"关闭TXTwoViewController....");
}];
//    [self.navigationController dismissViewControllerAnimated:YES completion:^{
//        NSLog(@"关闭TXTwoViewController....");
//    }];
}
@end


三、注意点

(1)modal的特点:当modal窗口弹出(从下往上)的时候,后面的视图不可点
(2)弹出控制器的视图(通过这种方式只能弹出一个视图)

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


(3)移除控制器的视图(两种方式都可以)

//编写点击返回按钮的点击事件
//点击返回按钮,移除当前模态窗口
//    [self.navigationController dismissViewControllerAnimated:YES completion:^{
//        NSLog(@"移除模态窗口");
//    }];

// 如果一个控制器是以模态的形式展现出来的, 可以调用该控制器以及该控制器的子控制器让让控制器消失
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"移除");
}];


(4)提示在实际的开发中,如果控制器之间的关系紧密一般用导航控制器,如果控制器之间的关系不是很紧密就用modal

四、内部机制
(1)弹出之后,window上面只有一个子视图。
(2)虽然当前界面上展示在我们眼前的时twoview,但是window的根控制器仍然是NJviewController,它并没有切换window的根控制器,而仅仅只是换了window上面显示的视图。
(3)移除的视图并没有销毁,因为控制器并没有销毁,所以控制器对应的view也没有销毁。
(4)在模态弹出(完全显示后),在方法中传入two作为参数,默认就有一个控制器强引用着它。
(5)当向下移除之后,只要调用了控制器的dismiss方法让窗口关闭,modal就释放了。
(6)通常弹出的模态窗口都会提供一个导航条,让界面拥有导航条的最快的方式是给它包装一个导航控制器。
(7)如果一个控制器是以模态的形式展现出来的。可以调用该控制器以及该控制器的子控制器,让该控制器消失。

五、数据的传递

项目文件结构和storyboard



代码示例:

TXViewController.m文件

#import "TXViewController.h"
#import "TXTwoViewController.h"

@interface TXViewController ()

@end

@implementation TXViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
/*
如果控制器之间的关系比较紧密一般用 UINavigationController
如果控制器之间的关系不是很紧密可以用Modal
*/
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//通过segue跳转前,会调用这个方法,在这个方法中把数据传递给弹出来的模态窗口
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UINavigationController *nav = segue.destinationViewController;

TXTwoViewController *two = (TXTwoViewController *)nav.topViewController;

two.name = @"小说哥";

}

@end


TXtwoViewController.h文件

#import <UIKit/UIKit.h>

@interface TXTwoViewController : UIViewController
@property (nonatomic, copy) NSString *name;
@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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: