美文网首页
iOS中3种反向传值

iOS中3种反向传值

作者: _Jock羁 | 来源:发表于2016-08-26 09:57 被阅读49次

使用代理Delegate实现反向传值

1)定义协议.方法
2)声明一个属性,记录代理对象
3)在合适的时机,给代理方发消息

BViewController.h
#import <UIKit/UIKit.h>
@class BViewController;

/** 定义协议 */
@protocol BViewControllerDelegate <NSObject>

/** 定义方法 */
-(void)BViewController:(BViewController *)bViewController didFinishInputMsg:(NSString *)msg;
@end

@interface BViewController : UIViewController

/** 声明一个属性,记录代理对象 */
@property (nonatomic,weak) id<BViewControllerDelegate> delegate;
@end
BViewController.m
@interface BViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end

@implementation BViewController
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)clickDelegate:(id)sender {
    /** 界面dismiss前给代理方发消息 */
    [self.delegate BViewController:self didFinishInputMsg:self.textField.text];
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
AViewController.m

1)遵守协议
2)实现方法
3)设置代理

AViewController.m
#import "AViewController.h"
#import "BViewController.h"

/** 遵守BViewController协议 */
@interface AViewController ()<BViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *label;
@end

@implementation AViewController
- (void)viewDidLoad {
    [super viewDidLoad];
}

/** 获取storyboard segue 设置代理 */
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    BViewController *bViewContrller = segue.destinationViewController;
    bViewContrller.delegate = self;
}

#pragma mark - BViewControllerDelegate
/** 实现BViewControllerDelegate的方法 */
-(void)BViewController:(BViewController *)bViewController didFinishInputMsg:(NSString *)msg {
    self.label.text = msg;
}
@end

使用通知NSNotification实现反向传值

BViewController.h

1)声明全局通知名称
2)发送全局通知

#import <UIKit/UIKit.h>
/** 声明一个全局通知名称 */
#define kNotificationDidFinishInputMsg @"kNotificationDidFinishInputMsg"
BViewController.m
@interface BViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end

@implementation BViewController
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)clickNotification:(id)sender {
    /** 发送全局通知 */
    [[NSNotificationCenter defaultCenter]postNotificationName:kNotificationDidFinishInputMsg object:@{@"msg" : self.textField.text}];
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
AViewController.m

1)收听通知
2)注销通知

#import "AViewController.h"
#import "BViewController.h"

@interface AViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@end

@implementation AViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    /** 添加收听者 */
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getNotification:) name:kNotificationDidFinishInputMsg object:nil];    
}

/** 当收到通知,实现该方法 */
-(void)getNotification:(NSNotification *)notification {
    self.label.text = notification.object[@"msg"];
}

/** dealloc方法里需注销通知 */
-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end

使用block实现反向传值

BViewController.h

1)声明block
2)传参

#import <UIKit/UIKit.h>

@interface BViewController : UIViewController
/** 声明block */
@property (nonatomic,copy) void (^finishInputMsgBlock)(NSString *msg);
@end
BViewController.m
#import "BViewController.h"
@interface BViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end

@implementation BViewController
- (void)viewDidLoad {
    [super viewDidLoad];
}
- (IBAction)clickBlock:(id)sender {
    /** 传入参数 */
    self.finishInputMsgBlock(self.textField.text);
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
AViewController.m

1)推出界面时回调block

#import "AViewController.h"
#import "BViewController.h"
@interface AViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@end

@implementation AViewController
- (void)viewDidLoad {
    [super viewDidLoad];
}

/** 获取storyboard segue 回调block */
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    BViewController *bViewContrller = segue.destinationViewController;
    bViewContrller.finishInputMsgBlock = ^(NSString *msg) {
        self.label.text = msg;
    };
}
@end

相关文章

  • 【iOS开发细节】之- delegate代理的使用

    在iOS开发中、好多时候需要涉及到页面传值、而传值又分为正向传值和反向传值 一、 传值 1、正向传值 2、反向传值...

  • iOS反向传值整理

    本文主要讲解iOS中常见的反向传值方法。 1.AppDelegate传值 在AppDelegate中定义相关的属性...

  • iOS中传值问题

    1.在iOS中正向传值 可以用属性就不多说了,反向传值 从image中传值给controller的时候 我们可以用...

  • ioS 页面(代理、通知、block、单例、属性)传值

    iOS 页面(代理、通知、block、单例、属性)传值 一、传值分类 页面传值基本分为两种:正向传值和反向传值。 ...

  • iOS 页面(代理、通知、block、单例、属性)传值

    iOS 页面(代理、通知、block、单例、属性)传值 一、传值分类 页面传值基本分为两种:正向传值和反向传值。 ...

  • iOS回调,代理和block

    iOS中我们经常会遇到正反向传值,正向传值就不用多介绍了,就是属性传值(当然也可以有其他方式,这里不做介绍了). ...

  • OC中反向传值的方法

    oc中反向传值四种方法 block反向传值 在需要传值的界面: 在接受到传值的界面 单例反向传值 创建一个单例类 ...

  • iOS (反向)传值

    代理、block、消息中心、单例。 正向传值 通过属性(特性)的值,在上个使用本类(所在类)对象的类中,直接传递其...

  • ios反向传值

    方法三:代码块 根视图代码: //.m文件 -(void)viewDidLoad { [superviewDidL...

  • iOS反向传值

    常用的方式 代理 block 通知 也可以使用以下方式但不推荐 单例 全局变量 NSUserDefaults 文件...

网友评论

      本文标题:iOS中3种反向传值

      本文链接:https://www.haomeiwen.com/subject/ebaasttx.html