使用代理进行反向传值
Posted xiao了C
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用代理进行反向传值相关的知识,希望对你有一定的参考价值。
目标:在两个独立的控制器的界面之间进行反向传值
关键技术:代理
代码编写及运行环境: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
对代理的一些想法:
代理的设计真的很美,逻辑特别清晰,任务也非常明确。使用代理技术,需要做到两点,第一点是设置协议,第二点是实现协议。
以上是关于使用代理进行反向传值的主要内容,如果未能解决你的问题,请参考以下文章