IOS学习 ModalViewController模态视图的创建 两视图间用代理方式<传值>

#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.

}


@end


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值