Also studied the next RAC, this is indeed a temporary difficult to learn, are all kinds of flow of practice. Flow to flow away, signaling mechanism. Nonsense not to say, directly on the code--can also go to my GitHub Click to open the link
#import "ViewController.h" #import "RWDummySigninService.h" #import <ReactiveCocoa/ReactiveCocoa.h> @interface Viewcontroller () {Uitextfield *_text; Uitextfield *_user; Uitextfield *_code; UIButton *_sig; } @property (strong,nonatomic) rwdummysigninservice *signinservice; @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload]; _text = [[Uitextfield alloc]init];//_text.frame = CGRectMake (+, +, +);//[Self.view Addsubview:_te XT]; [_text.rac_textsignal subscribenext:^ (ID x) {/////NSLog (@ "%@", x);/}]; Filter: Filter (for complex situations)//rac_textsignal: Is the category added by RAC for Uitextfield, and this value will be returned (Sendnext) as long as the value of Usernametextfield changes. --Create and return a text field for a signal//subscribenext: After adding the following code, signal is in hot state, and the code in the block will be executed. Nothing will happen if it is cold. [[_text.rac_textsignal Filter:^bool (id value) {nsstring *text = value; return Text.length >3; }]subscribenext:^ (id x) {NSLog (@ "%@", X); }]; ; Map----map NSString to NSNumber//The newly added map operation uses the provided block to transform the event data. For each next event that is received, the given block is run and the return value is sent as the next event. In the above code, the map operation gets a nsstring input, maps it to a NSNumber object, and returns. [[[_text.rac_textsignal Map:^id (NSString *text) {////Return @ (text.length);//}]//Filter:^boo L (NSNumber *length) {//return [length Intvalue] >3;//}]//subscribenext:^ (id x) {//NSLog (@ "%@", X );// }]; _user = [[Uitextfield alloc]init]; _user.frame = CGRectMake (100, 100, 100,30); _user.borderstyle = Uitextborderstyleline; [Self.view Addsubview:_user]; _code = [[Uitextfield alloc]init]; _code.frame = CGRectMake (100, 150, 100,30); _code.borderstyle = Uitextborderstyleline; [Self.view Addsubview:_code]; _sig = [UIButton buttonwithtype:uibuttontypecustom]; _sig.frame = CGRectMake (100, 250, 50, 30); _sig.backgroundcolor= [Uicolor Redcolor]; [Self.view Addsubview:_sig]; A map (mapped to nsnumber) conversion is applied to the rac_textsignal of each input box. The output is a Boolean value encapsulated in NSNumber. Then use the received value to change the color racsignal *validusernamesignal = [_user.rac_textsignal map:^id (NSString *text) {//return @ ([self Isvalidusername:text]); return nil; }]; Racsignal *validpasswordsignal = [_code.rac_textsignal map:^id (NSString *text) {//return @ ([self isValidPassword:t EXT]); return nil; }]; One way//[[Validpasswordsignal Map:^id (NSNumber *passwordvalid) {//return [Passwordvalid boolvalue]? [ Uicolor clearcolor]:[uicolor yellowcolor];////}] subscribenext:^ (Uicolor *color) {//_code.backgroundco Lor = color;//}]; This is a better notation for RAC (_code,backgroundcolor) = [validpasswordsignal map:^id (NSNumber *passwordvalid) {return [password Valid Boolvalue]? [Uicolor Clearcolor]:[uicolor Yellowcolor]; }]; The next step is to convert the signals so that the input boxes can be set to differentBackground color. Basically, you subscribe to these signals and then use the received values to update the background color of the input box//rac macros allow you to directly apply the output of the signal to the properties of the object. The RAC macro has two parameters, the first is an object that needs to set the property value (Nilvalue), and the second is the property name (TARGET). Each time a signal generates a next event, the passed value is applied to the property. Rac_ (TARGET, KeyPath, Nilvalue)//This macro is most commonly used, and RAC () always appears to the left of the equals sign, and the right side of the equals sign is a racsignal, meaning that a property of an object is bound to a signal, Signal automatically executes RAC (_user,backgroundcolor) = [validusernamesignal map:^id (NSNumber *passwordvalid), each resulting in a value (ID type) { return [Passwordvalid Boolvalue]? [Uicolor Clearcolor]: [Uicolor Yellowcolor]; }]; Combining signals//using the Combinelatest:reduce: method to combine the values of the last output of validusernamesignal and validpasswordsignal and generate a new signal. Each time a new value is output from two source signals, the reduce block is executed, and the returned value is the next value of the combined signal. Note: The Racsignal combination method can combine any number of signals, while the parameters of the reduce block correspond to each signal. Racsignal *signupactivesignal = [racsignal combinelatest:@[validusernamesignal,validpasswordsignal] Reduce:^id ( NSNumber *usernamevalid, NSNumber *passwordvalid) {return @ ([usernamevalid boolvalue] && [PASSWORDV Alid Boolvalue]); }]; This connects the signal to the enabled genus of the buttonOf In hot state//[Signupactivesignal subscribenext:^ (NSNumber *signupactive) {////_sig.enabled = [signupacti ve boolvalue];////NSLog (@ "11");//}]; Creates a signal from the UIControlEventTouchUpInside event of the button and adds a subscription to add a log each time the event occurs. [[_sig Rac_signalforcontrolevents:uicontroleventtouchupinside] subscribenext:^ (ID x) {////NSLog (@ "Bu Tton clicked ");//}]; Use the Map method to convert a button click Signal to a login signal. Subscribers simply output the results. [[[_sig Rac_signalforcontrolevents:uicontroleventtouchupinside] Flattenmap:^racstream * (id value) {return [self si Gninsignal]; }] subscribenext:^ (id x) {NSLog (@ "Sign in result:%@", X); }];} -(racsignal *) signinsignal{//createsignal: Method used to create a signal. The block describing the signal is a signal parameter, and when the signal has a subscriber, the code in the block is executed. return [racsignal createsignal:^racdisposable * (id<racsubscriber> subscriber) {[Self.signinservice s Igninwithusername:_user.text Password:_code.text complete:^ (BOOL success) { [Subscriber sendnext:@ (Success)]; [Subscriber sendcompleted]; }]; return nil; }]; } @end
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
An approach to IOS RAC