Help us understand the problem. What is going on with this article?

逆引きReactiveCocoa: @YES/@NOをトグルで送るRACCommandを用意する

More than 5 years have passed since last update.

元ネタ: Command which sends alternating YES/NO values · Issue #767 · ReactiveCocoa/ReactiveCocoa


ボタン・ジェスチャーなどのUI操作とRACSignalを仲介するクラスとしてRACCommandがあります。これを用いて、状態(フラグとなるインスタンス変数/プロパティ)を持たずにトグルボタンを表現するパターンです。

// ブロックの戻り値が`[command execute:senderOrValue]`の戻り値となる。
RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^(id _) {
    return [RACSignal return:RACUnit.defaultUnit];
}];

RACSignal *alternating = [[[command.executionSignals // `-execute:`の度にその戻り値のシグナルを送るシグナル。
    flatten]
    startWith:RACUnit.defaultUnit] // ユーザー操作前に初期値がセットされるようにする。
    scanWithStart:@NO combine:^(NSNumber *previous, id _) {
        // 前回の値を反転したブール値を返し、
        // コマンドの実行によって得られた値は無視する(_ == RACUnit.defaultUnit)。
        //
        // 最初に`@NO`を反転するため、シグナルの初回の値は`@YES`となる。
        return @(!previous.boolValue);
    }];
RAC(self, onOff) = alternating;

// `UIControlEventTouchUpInside`のイベントで`-execute:`される。
button.rac_command = command;
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away