Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind UISwitch's state to NSUserDefaults with ReactiveCocoa

How can I bind UISwitch.on with the value for a key in NSUserDefaults?

like image 228
Dmitry Avatar asked Jan 11 '23 19:01

Dmitry


1 Answers

You can bind them (two-way binding) by following:

RACChannelTerminal *switchTerminal = self.someSwitch.rac_newOnChannel;
RACChannelTerminal *defaultsTerminal = [[NSUserDefaults standardUserDefaults] rac_channelTerminalForKey:@"someBoolKey"];

[switchTerminal subscribe:defaultsTerminal];
[defaultsTerminal subscribe:switchTerminal];

The switch's on state starts with the value from the user defaults or NO.

like image 199
ikesyo Avatar answered Jan 30 '23 22:01

ikesyo