休息够了,该写点东西了,前一段时间感冒,发烧,扁桃体发炎,发烧,扁桃体再次发炎,再次发烧,够够的了,进入正题,这次主题是通过代理来实现传值,是逆传,就是反方向传值(废话),准备工作的是,有两个控制器,每个控制器上有两个控件,一个是Button(用来实现控制器之间的跳转),一个是Label(用来展示要传递的值和传递过来的值),代理传值很实用,很多时候用代理解耦,不过代码量也不少;
基本原理:有控制器A,控制器B,代理C,当B快要死的时候找了代理C,说:我快死了,你把我这点遗产给A送过去,我是没机会了,我全权委托你把这些钱送给A,然后C快马加鞭,送给了A。正式因为B不能做,所以找了代理C,C说:给我100w吧,要不然我就。。。。。。。
我用了VIewController(我比较懒);
ViewContoller.m
#import "ViewController.h"
#import "AirViewController.h"
@interface ViewController ()<AirViewControllerDelegate>
/**
* 用来显示传递过来的值
*/
@property (nonatomic,weak) UILabel *textLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupSubviews];
}
- (void)setupSubviews
{
//初始化一个按钮
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
btn.backgroundColor = [UIColor grayColor];
[btn setTitle:@"下一页" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
//初始化显示传递过来值得label
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, self.view.bounds.size.width, 40)];
textLabel.text = @"毛都没有";
textLabel.textAlignment = NSTextAlignmentCenter;
textLabel.backgroundColor = [UIColor greenColor];
[self.view addSubview:textLabel];
self.textLabel = textLabel;
}
//点击按钮事件
- (void)btnClick
{
AirViewController *nextVC = [[AirViewController alloc] init];
nextVC.delegate = self;
[self presentViewController:nextVC animated:YES completion:nil];
}
#pragma mark -
#pragma mark - AirViewControllerDelegate的方法
-(void)airViewController:(AirViewController *)controleller andValue:(NSString *)myStr
{
self.textLabel.text = myStr;
}
@end
效果图如下:
AirViewController.h
#import <UIKit/UIKit.h>
@class AirViewController;
@protocol AirViewControllerDelegate <NSObject>
/**
* 通知传值
*
* @param controleller self
* @param myStr 要传的字符串,根据需求可以更改类型
*/
- (void)airViewController:(AirViewController *)controleller andValue:(NSString *)myStr;
@end
@interface AirViewController : UIViewController
/**
* 设置代理(注意:一定是weak)
*/
@property (nonatomic,weak) id<AirViewControllerDelegate> delegate;
@end
AirViewController.m
#import "AirViewController.h"
@interface AirViewController ()
@property (nonatomic,weak) UILabel *myLabel;
@end
@implementation AirViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self setupSubviews];
}
- (void)setupSubviews
{
//返回按钮,点击返回按钮的时候通知传递mylabel上面的的字符串
UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(100, 400, 100, 100)];
backBtn.backgroundColor = [UIColor grayColor];
[backBtn setTitle:@"上一页" forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBtn];
//
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, self.view.bounds.size.width, 40)];
textLabel.textAlignment = NSTextAlignmentCenter;
textLabel.backgroundColor = [UIColor greenColor];
textLabel.text = @"welcome to my blog!";
[self.view addSubview:textLabel];
self.myLabel = textLabel;
}
- (void)backBtnClick
{
if ([self.delegate respondsToSelector:@selector(airViewController:andValue:)])
{
[self.delegate airViewController:self andValue:self.myLabel.text];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
效果图如下:
当我点击@“上一页”按钮的时候,奇迹出现了,如下图:
此时,不是@毛都没有了,而是有了一句高端大气的上档次的英文,这就是代理传值,传递的可以是任何对象;
如果转载请注明转于:AirZilong的博客