#ifndef Protocol_h
#define Protocol_h
#define VIEW_WIDTH self.view.bounds.size.width
#define VIEW_HEIGHT self.view.bounds.size.height
//设置代理,将textField传值给label
@protocol ModalViewControllerDelegate <NSObject>
- (void)changLabelText:(NSString *)text;
@end
#endif /* Protocol_h */
#import <UIKit/UIKit.h>
#import "ModalViewController.h"
#import "Protocol.h"
@interface HomeViewController : UIViewController<ModalViewControllerDelegate>
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(VIEW_WIDTH/2-100, 160, 200, 40)];
label.backgroundColor = [UIColor darkGrayColor];
label.tag = 102;
[self.view addSubview:label];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(VIEW_WIDTH/2-50, 300, 100, 40)];
[btn setTitle:@"present" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(presentModalVC) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor = [UIColor orangeColor];
btn.layer.cornerRadius = 6;
[self.view addSubview:btn];
}
- (void)presentModalVC{
//创建模态视图
ModalViewController *modalVC = [[ModalViewController alloc]init];
// modalVC.modalTransitionStyle = UIModalTransitionStylePartialCurl; //翻页的类型
modalVC.view.backgroundColor = [UIColor cyanColor];
//设置代理
modalVC.delegate = self;
//弹出模态视图
if ([[UIDevice currentDevice].systemVersion floatValue]<6.0) //版本判断
{
[self presentModalViewController:modalVC animated:YES];
}else{
[self presentViewController:modalVC animated:YES completion:^{
NSLog(@"call back");}];
}
}
- (void)changLabelText:(NSString *)text{
//根据tag查找label
UILabel *label = (UILabel *)[self.view viewWithTag:102];
label.text = text;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
#import "Protocol.h"
@interface ModalViewController : UIViewController{
@private
UITextField *_textField; //设置全局变量
UIButton *btn;
}
@property (nonatomic,assign) id<ModalViewControllerDelegate>delegate; //代理设置
@end
@implementation ModalViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_textField = [[UITextField alloc]initWithFrame:CGRectMake(5, 160, VIEW_WIDTH-10, 40)];
_textField.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_textField];
btn = [[UIButton alloc]initWithFrame:CGRectMake(VIEW_WIDTH/2-50, 300, 100, 40)];
[btn setTitle:@"dismiss" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor = [UIColor purpleColor];
btn.layer.cornerRadius = 6;
[self.view addSubview:btn];
}
- (void)dismiss{
//判断是否能调通这个方法
if ([self.delegate respondsToSelector:@selector(changLabelText:)])
{
//改变label的内容
[self.delegate changLabelText:_textField.text];
}
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"dismiss");}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}