模態:模態視圖從屏幕下方滑出來,完成的時候需要關閉這個模態視圖,如果不關閉,就不能做別的事情,必須有響應處理的含義。
主視圖控制器---》模態視圖控制器。主視圖控制器與模態視圖控制器之間為父子關系。
UIViewController類中,主要有以下兩個方法:
presentViewController:animated:completion 呈現模態視圖
dismissViewControllerAnimated:completion 關閉模態視圖
代碼:
ViewController.h
#import @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 @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