模态:模态视图从屏幕下方滑出来,完成的时候需要关闭这个模态视图,如果不关闭,就不能做别的事情,必须有响应处理的含义。
主视图控制器---》模态视图控制器。主视图控制器与模态视图控制器之间为父子关系。
UIViewController类中,主要有以下两个方法:
presentViewController:animated:completion 显现模态视图
dismissViewControllerAnimated:completion 关闭模态视图
代码:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
- (IBAction)regonclick:(id)sender;
@end
ViewController.m
#import "ViewController.h"
#import "RegisterViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 如果要传递参数的时候,应当用拜托设计模式或广播通知机制
// 这里为广播通知机制
// 注册1个自定义通知RegisterCompletionNotification,通知到来时候发出registerCompletion:消息,其参数notification中可以包括回传的参数,统1放在NSDictionary字典中
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerCompletion:) name:@"RegisterCompletionNotification" object:nil];
}
- (void)registerCompletion:(NSNotification *)notification
{
// 获得参数userInfo,是1个字典
NSDictionary *theData = [notification userInfo];
// 获得字典的username索引值
NSString *username = [theData objectForKey:@"username"];
NSLog(@"username = %@", username);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)regonclick:(id)sender {
// 使用registerViewController的ID获得视图控制器对象
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *registerViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"registerViewController"];
// 设定模态视图显现和关闭时候的动画效果
// 垂直方向由底向上退出
registerViewController.modalPresentationStyle = UIModalTransitionStyleCoverVertical;
// 显现完成时调用completion代码块
// 代码块问题回头再说
[self presentViewController:registerViewController animated:YES completion:^{
NSLog(@"Present Modal View");
}];
}
@end
ResgisterViewController.h
#import <UIKit/UIKit.h>
@interface RegisterViewController : UIViewController
- (IBAction)done:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *txtUsername;
@end
ResgisterViewController.m
#import "RegisterViewController.h"
@interface RegisterViewController ()
@end
@implementation RegisterViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (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)done:(id)sender {
// 关闭模态视图,然后继续调用代码块
[self dismissViewControllerAnimated:YES completion:^{
// 代码块
NSLog(@"Modal View done");
// 创建1个以username为索引的字典对象
NSDictionary *dataDict = [NSDictionary dictionaryWithObject:self.txtUsername.text forKey:@"username"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"RegisterCompletionNotification" object:nil
userInfo:dataDict];
}];
}
@end