iOS 代理传值(逆传)
2016-05-08 12:53
806 查看
休息够了,该写点东西了,前一段时间感冒,发烧,扁桃体发炎,发烧,扁桃体再次发炎,再次发烧,够够的了,进入正题,这次主题是通过代理来实现传值,是逆传,就是反方向传值(废话),准备工作的是,有两个控制器,每个控制器上有两个控件,一个是Button(用来实现控制器之间的跳转),一个是Label(用来展示要传递的值和传递过来的值),代理传值很实用,很多时候用代理解耦,不过代码量也不少;
基本原理:有控制器A,控制器B,代理C,当B快要死的时候找了代理C,说:我快死了,你把我这点遗产给A送过去,我是没机会了,我全权委托你把这些钱送给A,然后C快马加鞭,送给了A。正式因为B不能做,所以找了代理C,C说:给我100w吧,要不然我就。。。。。。。
我用了VIewController(我比较懒);
ViewContoller.m
效果图如下:
AirViewController.h
AirViewController.m
当我点击@“上一页”按钮的时候,奇迹出现了,如下图:
此时,不是@毛都没有了,而是有了一句高端大气的上档次的英文,这就是代理传值,传递的可以是任何对象;
如果转载请注明转于:AirZilong的博客
基本原理:有控制器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的博客
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- flex 控件的重要属性
- Delphi控件ListView的属性及使用方法详解
- web下载的ActiveX控件自动更新
- WinForm实现按名称递归查找控件的方法
- C#中父窗口和子窗口之间控件互操作实例
- Android编程之Button控件用法实例分析
- Android控件之CheckBox、RadioButton用法实例分析
- 在Android开发中使用自定义组合控件的例子
- Android重写View实现全新的控件
- MFC中动态创建控件以及事件响应实现方法
- WinForm自定义函数FindControl实现按名称查找控件
- Android控件之ProgressBar用法实例分析
- winform异型不规则界面设计的实现方法