首页 > 代码库 > 模态视图(IOS开发)

模态视图(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 = http://www.mamicode.com/[notification userInfo];>
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


模态视图(IOS开发)