项目开发当中往往会遇到控制器之间的传值,总结了一下总共有如下8种传递方式(包括顺传和逆传)
代理 、 单粒 、通知 、 kvc 、 block 、 NSUserDefaults 、 Target-Action 和 属性方法传值 这8种方式
2.直接上代码,今后有时间再整理
storyboard中的结构
运行结果:
//
// FirstViewController.m
// pastValue
//
// Created by Vitco on 16/4/27.
// Copyright © 2016年 ting. All rights reserved.
//
#import "FirstViewController.h"
#import "SecondVController.h"
#import "Singleton.h"
@interface FirstViewController()
@property(copy,nonatomic)NSString *ValueStr;
- (IBAction)SecondVCwithdelegate:(id)sender;
- (IBAction)SecondVCwithBlock:(id)sender;
- (IBAction)SecondVCWithKVC:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *ValueLabel;
- (IBAction)SecondVCWithNotification:(id)sender;
- (IBAction)SecondVCWithSingle:(id)sender;
- (IBAction)SecondVCWith:(id)sender;
- (IBAction)SecondVCWithTargetAction:(id)sender;
@property(assign,nonatomic)BOOL kvcFlag;
@end
@implementation FirstViewController
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
Singleton * single = [Singleton sharedSingleton];
if (single.valueStr.length !=0) {
self.ValueLabel.text = single.valueStr;
}
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"valueStr"] length] != 0) {
self.ValueLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"valueStr"];
//获得了传递过来的数据之后要清空
[[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"valueStr"];
}
}
-(void)viewDidLoad
{
[super viewDidLoad];
self.kvcFlag = false;
// self.view.backgroundColor = [UIColor redColor];
#pragma mark 通过通知机制逆向传递数据
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ValueStrNotification:) name:@"ValueStrNotification" object:nil];
}
- (IBAction)SecondVCwithdelegate:(id)sender {
SecondVController * second = [self.storyboard instantiateViewControllerWithIdentifier:@"second"];
second.delegate = self;
[self presentViewController:second animated:YES completion:nil];
}
- (IBAction)SecondVCwithBlock:(id)sender {
SecondVController * second = [self.storyboard instantiateViewControllerWithIdentifier:@"second"];
[self presentViewController:second animated:YES completion:nil];
second.block = ^(NSString *str){
self.ValueLabel.text = str;
};
}
- (IBAction)SecondVCWithKVC:(id)sender {
self.kvcFlag = true;
// SecondVController * second = [self.storyboard instantiateViewControllerWithIdentifier:@"second"];
//
// [self presentViewController:second animated:YES completion:nil];
[self performSegueWithIdentifier:@"2withkvc" sender:nil];
}
- (IBAction)SecondVCWithNotification:(id)sender {
SecondVController * second = [self.storyboard instantiateViewControllerWithIdentifier:@"second"];
[self presentViewController:second animated:YES completion:nil];
}
- (IBAction)SecondVCWithSingle:(id)sender {
SecondVController * second = [self.storyboard instantiateViewControllerWithIdentifier:@"second"];
[self presentViewController:second animated:YES completion:nil];
}
//userdefault传值
- (IBAction)SecondVCWith:(id)sender {
SecondVController * second = [self.storyboard instantiateViewControllerWithIdentifier:@"second"];
[self presentViewController:second animated:YES completion:nil];
}
- (IBAction)SecondVCWithTargetAction:(id)sender {
// SecondVController * second = [[HMTShowViewController alloc]init];
// showVC.traget = self;
// showVC.action = @selector(changeLabelText:);
SecondVController * second = [self.storyboard instantiateViewControllerWithIdentifier:@"second"];
second.target = self;
second.action = @selector(valueString:);
[self presentViewController:second animated:YES completion:nil];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// _kvcFlag = true;
if (_kvcFlag) {
UIViewController *vc = [segue destinationViewController];
[vc setValue:@"kvc传值" forKey:@"kvcStr"];
}
}
#pragma mark 通知机制逆向传递数据
-(void)ValueStrNotification:(NSNotification*)notification{
// notification.object为通知的发布者
NSDictionary *nameDictionary = [notification userInfo];
self.ValueLabel.text = [nameDictionary objectForKey:@"name"];
}
-(void)gobackWithdelegate:(SecondVController *)SecondV pastValue:(NSString *)valeStr
{
self.ValueLabel.text = valeStr;
}
-(void)valueString:(NSString *)str
{
_ValueLabel.text = str;
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
//
// SecondVController.h
// pastValue
//
// Created by Vitco on 16/4/27.
// Copyright © 2016年 ting. All rights reserved.
//
#import
typedef void (^valueblock)(NSString *valustr);
@class SecondVController ;
@protocol SecondVControllerDelegate
- (void)gobackWithdelegate:(SecondVController *)SecondV pastValue:(NSString *)valeStr;
@end
@interface SecondVController : UIViewController
@property(nonatomic,copy)NSString *kvcStr;
@property(weak,nonatomic)id delegate;
@property(copy,nonatomic)valueblock block;
@property (nonatomic,assign)id target;
@property (nonatomic,assign)SEL action;
@end
// // SecondVController.m // pastValue // // Created by Vitco on 16/4/27. // Copyright © 2016年 ting. All rights reserved. // #import "SecondVController.h" #import "Singleton.h" @interface SecondVController() - (IBAction)GobackWithdelegate:(id)sender; - (IBAction)GobackWithnotification:(id)sender; - (IBAction)GobackWithSingle:(id)sender; - (IBAction)GobackWithBlock:(id)sender; - (IBAction)GobackWithuserDefault:(id)sender; @property (weak, nonatomic) IBOutlet UILabel *secondLabel; - (IBAction)GobackWithTargetAction:(id)sender; @end @implementation SecondVController -(void)viewDidLoad { [super viewDidLoad]; self.secondLabel.text = _kvcStr; } - (IBAction)GobackWithdelegate:(id)sender{ if ([self.delegate respondsToSelector:@selector(gobackWithdelegate:pastValue:)]) { [self.delegate gobackWithdelegate:self pastValue:@"代理传值"]; } [self dismissViewControllerAnimated:YES completion:nil]; } - (IBAction)GobackWithnotification:(id)sender{ [[NSNotificationCenter defaultCenter]postNotificationName:@"ValueStrNotification" object:self userInfo:@{@"name":@"通知传值"}]; [self dismissViewControllerAnimated:YES completion:nil]; } - (IBAction)GobackWithSingle:(id)sender{ Singleton * single = [Singleton sharedSingleton]; single.valueStr = @"单粒传值"; [self dismissViewControllerAnimated:YES completion:nil]; } - (IBAction)GobackWithBlock:(id)sender { if (self.block) { self.block(@"block传值"); [self dismissViewControllerAnimated:YES completion:nil]; } } - (IBAction)GobackWithuserDefault:(id)sender { [[NSUserDefaults standardUserDefaults] setObject:@"userDefault传值" forKey:@"valueStr" ]; [self dismissViewControllerAnimated:YES completion:nil]; } - (IBAction)GobackWithTargetAction:(id)sender { [self.target performSelector:self.action withObject:@"Target-Action传值"]; [self dismissViewControllerAnimated:YES completion:nil]; } @end