ReactiveCocoa 两个开关互异绑定怎么写?

假设有两个 Siwtch 需要互异绑定,用 rac 该怎么写?

UISwitch *switch1;
UISwitch *switch2;

switch1.action = {
    switch2.on = !switch2.on;
}

switch2.action = {
    switch1.on = !switch1.on;
}
阅读 4.1k
2 个回答
新手上路,请多包涵

这样?

switch1.on = YES;
switch2.on = NO;
RACChannelTerminal *t1 = switch1.rac_newOnChannel;
RACChannelTerminal *t2 = switch2.rac_newOnChannel;
[[t1 map:^id(NSNumber *on) {
    return @(![on boolValue]);
}] subscribe:t2];
[[t2 map:^id(NSNumber *on) {
    return @(![on boolValue]);
}] subscribe:t1];

找到一种解决方案:

RACChannelTerminal *aButtonChannel = [aButton rac_newSelectedChannel];
RACChannelTerminal *bButtonChannel = [bButton rac_newSelectedChannel];
[[aButtonChannel not] subscribe:bButtonChannel];
[[bButtonChannel not] subscribe:aButtonChannel];
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题