Objective:
In a previous article, Reactivecocoa is a function-responsive programming, and unifies various event responses to make different event responses highly uniform. At the same time, we also talked about several common concepts in the REACTIVECOCOA framework. Next, we look at several applications in UI development based on those concepts.
Combat: 1, replaced UIButton's target-action:
1 [[self.btn rac_signalforcontrolevents: (uicontroleventtouchupinside)] subscribenext:^(id x) { 2 NSLog (@ " clicked button"); 3 }];
Go to the rac_signalforcontrolevents method to see how this method is done:
1-(Racsignal *) Rac_signalforcontrolevents: (uicontrolevents) controlevents {2 @weakify (self);3 4 return[[Racsignal5createsignal:^ (id<racsubscriber>subscriber) {6 @strongify (self);7 8 [Self addtarget:subscriber Action: @selector (Sendnext:) forcontrolevents:controlevents];9[Self.rac_deallocdisposable adddisposable:[racdisposable disposablewithblock:^{Ten [subscriber sendcompleted]; One }]]; A - return[Racdisposable disposablewithblock:^{ - @strongify (self); the [Self removetarget:subscriber Action: @selector (Sendnext:) forcontrolevents:controlevents]; - }]; - }] -Setnamewithformat:@"%@-rac_signalforcontrolevents:%lx", Racdescription (self), (unsignedLong) controlevents]; +}
You can see that a racsignal signal is created and the action is added to the button when the block callback is performed. This method returns a racsignal signal, while we call Subscribenext to subscribe to the signal, when the button is clicked to call the Sendnext method to send the value of the callback Subscribenext block.
2, binding TextView monitoring (using TextField the same reason)
1 [Self.myTextView.rac_textSignal subscribenext:^(id x) {2 NSLog (@ " output:%@ " , x); 3 }];
3, bind gesture:
1 New ]; 2 [Self.redview Addgesturerecognizer:tap]; 3 [Tap.rac_gesturesignal subscribenext:^(id x) {4 NSLog (@ " click on the Red View " ); 5 }];
You can feel that some of the more mundane UI controls are based on Reactivecocoa or relatively simple, and it's worth saying that when the UI control is the proxy way to listen to the response process. Like Uiimagepicker. The following code to achieve a simple small function, click the button to select the picture, the image is selected after the display on the Uiimageview.
4, replace the proxy callback for the UI control:
1[[Self.btn rac_signalforcontrolevents: (uicontroleventtouchupinside)] subscribenext:^(id x) {2 3 //Click the button to eject Uiimagepicker4Self.imagepicker = [UiimagepickercontrollerNew];5[Self.imagePicker.rac_imageSelectedSignal subscribenext:^(id x) {6 //The block callback is called when the photo selection is complete7NSLog (@"%@", x);8Nsdictionary *dic = (Nsdictionary *) x;9Self.myImageView.image = dic[@"Uiimagepickercontrolleroriginalimage"];Ten [Self.imagepicker dismissviewcontrolleranimated:yes completion:nil]; One }]; ARac_delegateproxy is the proxy attribute under RAC, which can be understood as the proxy under the RAC will execute the block callback to replace the previous agent to execute the Imagepickercontrollerdidcancel method
- [[Self.imagePicker.rac_delegateProxy Signalforselector: @selector (imagepickercontrollerdidcancel:)] Subscribenext :^
- // The block call time: When delegate to execute Imagepickercontrollerdidcancel
15
16
17
];
Notice under 5,rac:
First page registration notification:
1 [[[Nsnotificationcenter Defaultcenter] Rac_addobserverforname:@ " changecolor " object : nil] subscribenext:^ (id x) { 2 nsnotification *notification = (nsnotification *) x; 3 NSLog (@ " receive notification:%@ , Notification. object 4 Self.view.backgroundColor = (Uicolor *) notification.object ; 5 }];
The second page Returns a button to send a notification:
[[Nsnotificationcenter Defaultcenter] Postnotificationname:@ "changecolor"Object : [Uicolor Graycolor]];
Observer design Patterns under 6,rac:
CurrentValue is a property of type int that the view Controller owns. Observe the change of this property
[Self Rac_valuesandchangesforkeypath:@ "currentvalue" options: ( nskeyvalueobservingoptionnew) observer:self] subscribenext:^(id x) { //Jiu Baoyuan Group, the values inside the tuple are assigned to the variable in order Ractupleunpack (nsstring *kind,nsstring *new) = x; NSLog (@ " observed that the value of CurrentValue has changed, now value equals%@,%@", Kind,new) ; }];
button Click to change the value of CurrentValue
[[Self.valuebutton rac_signalforcontrolevents: (uicontroleventtouchupinside)] subscribenext:^(id x) { ++; }];
Source code Address: Https://github.com/SZT0728/ReactiveCocoaProgram
You can see that the UI under RAC is highly uniform in how multiple events respond to block callbacks. The article is not the essence, only want to be able to write down their own learning and share in the simplest form. If there are any irregularities, please point out.
Reactivecocoa's UI Chapter