模態(tài)視圖(IOS開發(fā))
來源:程序員人生 發(fā)布時(shí)間:2014-11-17 08:53:10 閱讀次數(shù):3287次
模態(tài):模態(tài)視圖從屏幕下方滑出來,完成的時(shí)候需要關(guān)閉這個(gè)模態(tài)視圖,如果不關(guān)閉,就不能做別的事情,必須有響應(yīng)處理的含義。
主視圖控制器---》模態(tài)視圖控制器。主視圖控制器與模態(tài)視圖控制器之間為父子關(guān)系。
UIViewController類中,主要有以下兩個(gè)方法:
presentViewController:animated:completion 顯現(xiàn)模態(tài)視圖
dismissViewControllerAnimated:completion 關(guān)閉模態(tài)視圖

代碼:
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];
// 如果要傳遞參數(shù)的時(shí)候,應(yīng)當(dāng)用拜托設(shè)計(jì)模式或廣播通知機(jī)制
// 這里為廣播通知機(jī)制
// 注冊(cè)1個(gè)自定義通知RegisterCompletionNotification,通知到來時(shí)候發(fā)出registerCompletion:消息,其參數(shù)notification中可以包括回傳的參數(shù),統(tǒng)1放在NSDictionary字典中
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerCompletion:) name:@"RegisterCompletionNotification" object:nil];
}
- (void)registerCompletion:(NSNotification *)notification
{
// 獲得參數(shù)userInfo,是1個(gè)字典
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獲得視圖控制器對(duì)象
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *registerViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"registerViewController"];
// 設(shè)定模態(tài)視圖顯現(xiàn)和關(guān)閉時(shí)候的動(dòng)畫效果
// 垂直方向由底向上退出
registerViewController.modalPresentationStyle = UIModalTransitionStyleCoverVertical;
// 顯現(xiàn)完成時(shí)調(diào)用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 {
// 關(guān)閉模態(tài)視圖,然后繼續(xù)調(diào)用代碼塊
[self dismissViewControllerAnimated:YES completion:^{
// 代碼塊
NSLog(@"Modal View done");
// 創(chuàng)建1個(gè)以u(píng)sername為索引的字典對(duì)象
NSDictionary *dataDict = [NSDictionary dictionaryWithObject:self.txtUsername.text forKey:@"username"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"RegisterCompletionNotification" object:nil
userInfo:dataDict];
}];
}
@end
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)