使用代理进行反向传值

目标:在两个独立的控制器的界面之间进行反向传值

关键技术:代理

代码编写及运行环境:Xcode6.4 / 模拟器8.4

语言:Objective-C

注:使用纯代码实现,不使用xib/storyboard

效果图:

前期注意:

代码实现如下:

1.

 1 //
 2 //  AppDelegate.h
 3 //  delegatePassValue
 4 //
 5 //  Created by xiaoC on 16/9/28.
 6 //  Copyright (c) 2016年 xiaoLeC. All rights reserved.
 7 //
 8
 9 #import <UIKit/UIKit.h>
10
11 @interface AppDelegate : UIResponder <UIApplicationDelegate>
12
13 @property (strong, nonatomic) UIWindow *window;
14
15
16 @end
 1 //
 2 //  AppDelegate.m
 3 //  delegatePassValue
 4 //
 5 //  Created by xiaoC on 16/9/28.
 6 //  Copyright (c) 2016年 xiaoLeC. All rights reserved.
 7 //
 8
 9 #import "AppDelegate.h"
10 #import "ViewController.h"
11
12 @interface AppDelegate ()
13
14 @end
15
16 @implementation AppDelegate
17
18
19 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
20     // Override point for customization after application launch.
21
22     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
23     self.window.backgroundColor = [UIColor whiteColor];
24     [self.window makeKeyAndVisible];
25     self.window.rootViewController = [[ViewController alloc] init];
26
27
28     return YES;
29 }
30
31 - (void)applicationWillResignActive:(UIApplication *)application {
32     // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
33     // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
34 }
35
36 - (void)applicationDidEnterBackground:(UIApplication *)application {
37     // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
38     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
39 }
40
41 - (void)applicationWillEnterForeground:(UIApplication *)application {
42     // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
43 }
44
45 - (void)applicationDidBecomeActive:(UIApplication *)application {
46     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
47 }
48
49 - (void)applicationWillTerminate:(UIApplication *)application {
50     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
51 }
52
53 @end

2.

 1 //
 2 //  ViewController.h
 3 //  delegatePassValue
 4 //
 5 //  Created by xiaoC on 16/9/28.
 6 //  Copyright (c) 2016年 xiaoLeC. All rights reserved.
 7 //
 8
 9 #import <UIKit/UIKit.h>
10
11 @interface ViewController : UIViewController
12
13
14 @end
 1 //
 2 //  ViewController.m
 3 //  delegatePassValue
 4 //
 5 //  Created by xiaoC on 16/9/28.
 6 //  Copyright (c) 2016年 xiaoLeC. All rights reserved.
 7 //
 8
 9 #import "ViewController.h"
10 #import "YFSecondViewController.h"
11
12
13 @interface ViewController () <passValue> //1.遵守协议
14 @property(nonatomic,strong)UILabel *lab;
15
16 @end
17
18 @implementation ViewController
19
20 - (void)viewDidLoad {
21     [super viewDidLoad];
22     // Do any additional setup after loading the view, typically from a nib.
23     self.view.backgroundColor = [UIColor grayColor];
24
25     //添加一个跳转的按钮
26     UIButton *jumpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
27     jumpBtn.frame = CGRectMake(100, 200, 200, 50);
28     jumpBtn.backgroundColor = [UIColor yellowColor];
29     [jumpBtn setTitle:@"跳转" forState:UIControlStateNormal];
30     [jumpBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
31     [jumpBtn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
32     [self.view addSubview:jumpBtn];
33
34     //创建一个label,接收传回来的值
35     _lab = [[UILabel alloc] initWithFrame:CGRectMake(100, 400, 200, 50)];
36     _lab.backgroundColor = [UIColor yellowColor];
37     _lab.textAlignment = NSTextAlignmentCenter;
38     [self.view addSubview:_lab];
39
40 }
41
42 //调用该方法跳转到YFSecondViewController控制器界面
43 -(void)jump
44 {
45     YFSecondViewController *yfSecondV = [[YFSecondViewController alloc] init];
46     //2.设置代理
47     yfSecondV.delegate = self; //让ViewController控制器成为YFSecondViewController控制器的代理
48     [self presentViewController:yfSecondV animated:YES completion:nil];
49 }
50
51 //3.实现协议方法
52 -(void)backPassValue:(NSString *)str
53 {
54     //将返回来的值设置给label
55     _lab.text = str;
56 }
57
58
59 @end

3.

 1 //
 2 //  YFSecondViewController.h
 3 //
 4 //
 5 //  Created by xiaoC on 16/9/28.
 6 //
 7 //
 8
 9 #import <UIKit/UIKit.h>
10
11 @protocol passValue <NSObject>
12
13 -(void)backPassValue:(NSString *)str;
14
15 @end
16
17 @interface YFSecondViewController : UIViewController
18
19 @property(nonatomic,weak) id <passValue> delegate;
20
21 @end
 1 //
 2 //  YFSecondViewController.m
 3 //
 4 //
 5 //  Created by xiaoC on 16/9/28.
 6 //
 7 //
 8
 9 #import "YFSecondViewController.h"
10
11 @interface YFSecondViewController ()
12
13 @end
14
15 @implementation YFSecondViewController
16
17 - (void)viewDidLoad {
18     [super viewDidLoad];
19     self.view.backgroundColor = [UIColor orangeColor];
20
21     //来一些数据
22     NSArray *arr = @[@"深圳",@"上海",@"北京"];
23     for (int i=0; i<3; i++) {
24         UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
25         btn.frame = CGRectMake(100, (i*50)+80, 50, 40);
26         [btn setTitle:arr[i] forState:UIControlStateNormal];
27         [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
28         [btn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
29         [self.view addSubview:btn];
30     }
31
32
33 }
34
35 -(void)back:(UIButton *)sender
36 {
37     //判断协议中的方法有没有实现,有实现就执行if中的语句
38     if ([self.delegate respondsToSelector:@selector(backPassValue:)]) {
39
40         [self.delegate backPassValue:sender.currentTitle];
41         [self dismissViewControllerAnimated:YES completion:nil];
42     }
43 }
44
45 - (void)didReceiveMemoryWarning {
46     [super didReceiveMemoryWarning];
47     // Dispose of any resources that can be recreated.
48 }
49
50
51 @end

对代理的一些想法:

  代理的设计真的很美,逻辑特别清晰,任务也非常明确。使用代理技术,需要做到两点,第一点是设置协议,第二点是实现协议。

时间: 2024-08-25 10:46:28

使用代理进行反向传值的相关文章

iOS 代理反向传值

在上篇博客 iOS代理协议 中,侧重解析了委托代理协议的概念等,本文将侧重于它们在开发中的应用. 假如我们有一个需求如下:界面A上面有一个button.一个label.从界面A跳转到界面B,在界面B的输入框中输入字符串,在界面A的label上显示.这是一个典型的反向传值的例子.这个例子的核心是:"在界面B的输入框中输入字符串,在界面A的label上显示".也就是说:"界面B委托界面A显示字符串,页面A是界面B的代理".委托方向代理方反向传值. 那么我们该怎么用代理设

OC10_代理反向传值

// // ProtectedDelegate.h // OC10_代理反向传值 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> @protocol ProtectedDelegate <NSObject> - (void)bark; @end // // D

【iOS开发-26】利用协议代理实现导航控制器UINavigationController视图之间的正向传值和反向传值

实验说明 (1)正向传值:比如A类里地值要传给B类用,就是我们先在A类中声明一个B类对象(当然B类头文件要import过来),然后把A类中得某个值传递给B类中得某个值(所以需要在B类中先准备一个变量来接受,就是用@property和@synthesize整个变量即可). (2)反向传值:比如需要把B类中的值传递给A类用.我们先在B类中写一个协议,协议里面有一个可以带参数的方法,这个参数就是我们要传递的值(这个协议也可以单独写,不一定写在B类中),然后B类遵循这个协议后,利用这个协议创建一个委托变

ios 得用代理反向传值

应用场景:有时时候从界面A跳转到界面B,界面B在返回的时候需要将处理的结果传递给A. 实现思路:1,定义一个负责传值的协义,界面A拥有该协义属性,并实现该协义中的方法 2,界面B也拥有该协义属性(代理要求两者都具有相同对象的引用 ),然后在返回的时候获取界面A的引用指针,并且指定B中协义的调用目标为A,调用协义中的传值方法. 具体代码: A的头文件 : #import <UIKit/UIKit.h> @protocol passValueDelegate <NSObject> -(

控制器之间反向传值

控制器之间反向传值 委托(代理) 首先我们定下一个协议protocol 1. #import <Foundation/Foundation.h>2.3. @protocol ChangeText <NSObject>4.5. -(void)changeText:(NSString *)str;6. @end 控制器a遵守协议ChangeText,并实现协议的方法,控制器b公开自己的一个遵守协议ChangeText的属性delegate,在控制器a的视图转到控制器b的视图时,b.de

反向传值实例

反向传值实例 1.代理反向传值 #import <UIKit/UIKit.h> //声明一个类 @class LHTableViewController; //声明一个协议 @protocol LHTableViewControllerDelegate <NSObject> //协议中的方法 -(void)LHTablieViewController:(LHTableViewController *)LHTablieViewController Color:(UIColor *)c

代理和block反向传值

代理传值: // SendViewController.h #import <UIKit/UIKit.h> @protocol SendInFor <NSObject> -(void)sendInForIdea:(NSString*)text; @end @protocol SendInForTwo <NSObject> -(void)sender:(NSString*)text; @end @interface SendViewController : UIViewC

反向传值的几种常用方法

最近项目完成的差不多了,闲下来的时间突然心血来潮想自己写写以前没用过的方法.这里就包含了几种常见的反向传值的方法. 之所以现在专门自己写反向传值的博文记录,是因为之前几乎没怎么用到这些反向传值的方法,那么这里将会描述一下像"代理"."block"."通知"."单例"这四种传值方法. 首先是代理,也就是常看到的delegate.说的通俗点,就是委托一个对象,让其帮忙处理事情,说到底就是个中介的标志.详细方案如下: 在父类中调用其

iOS反向传值--Block方法

RootViewController代码如下: #import "RootViewController.h" #import "MyControl.h" #import "SecondViewController.h" #define kDebugPrint NSLog(@"%s",__func__) @interface RootViewController () {     UILabel *_label; } @end