模态视图(IOS开发)
模态:模态视图从屏幕下方滑出来,完成的时候需要关闭这个模态视图,如果不关闭,就不能做别的事情,必须有响应处理的含义。
主视图控制器---》模态视图控制器。主视图控制器与模态视图控制器之间为父子关系。
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]; // 如果要传递参数的时候,应该用委托设计模式或者广播通知机制 // 这里为广播通知机制 // 注册一个自定义通知RegisterCompletionNotification,通知到来时候发出registerCompletion:消息,其参数notification中可以包含回传的参数,统一放在NSDictionary字典中 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerCompletion:) name:@"RegisterCompletionNotification" object:nil]; } - (void)registerCompletion:(NSNotification *)notification { // 获取参数userInfo,是一个字典 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"); // 创建一个以username为索引的字典对象 NSDictionary *dataDict = [NSDictionary dictionaryWithObject:self.txtUsername.text forKey:@"username"]; [[NSNotificationCenter defaultCenter] postNotificationName:@"RegisterCompletionNotification" object:nil userInfo:dataDict]; }]; } @end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。