RAC的初次使用

2018-10-13  本文已影响0人  ldhonline

一、Pod 安装 RAC

  1. 在项目根目录初始化 pod
pod init

命令会自动创建一个 Podfile 文件。

  1. 在Podfile文件中添加依赖
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'

target 'uiview' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for uiview

pod 'ReactiveObjC', '~> 3.1.0'
pod 'Masonry', '~> 1.0.2'

end
  1. 安装依赖
pod install

项目会自动转换为 pod 管理模式,并安装好所有清单中的依赖。

  1. 添加 RAC 的头文件引用
#import "ReactiveObjecC.h"

二、使用 RAC 连接组件

  1. 使用 @weakinfo(self) 弱化引用, 在闭包中使用 @strongify(self) 强化引用。
- (void)bindComponents{
    @weakify(self);
    [[self.textView rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        @strongify(self);
        self.myView.text = x;
    }];
}
上一篇下一篇

猜你喜欢

热点阅读