ReactiveCocoa UI, reactivecocoaui

Source: Internet
Author: User

ReactiveCocoa UI, reactivecocoaui
Preface:

In the previous article, ReactiveCocoa is a function-responsive programming that unifies multiple event response methods, so that different event response methods are highly unified. I also talked about several common concepts in the ReactiveCocoa framework. Next, let's look at several applications in UI development based on those concepts.

Practice: 1. Replace the target-Action of UIButton:
1 [[self. btn rac_signalForControlEvents :( UIControlEventTouchUpInside)] subscribeNext: ^ (id x) {2 NSLog (@ "click button"); 3}];

Go to the rac_signalForControlEvents method to view the operations performed by this method:

 1 - (RACSignal *)rac_signalForControlEvents:(UIControlEvents)controlEvents { 2     @weakify(self); 3  4     return [[RACSignal 5         createSignal:^(id<RACSubscriber> subscriber) { 6             @strongify(self); 7  8             [self addTarget:subscriber action:@selector(sendNext:) forControlEvents:controlEvents]; 9             [self.rac_deallocDisposable addDisposable:[RACDisposable disposableWithBlock:^{10                 [subscriber sendCompleted];11             }]];12 13             return [RACDisposable disposableWithBlock:^{14                 @strongify(self);15                 [self removeTarget:subscriber action:@selector(sendNext:) forControlEvents:controlEvents];16             }];17         }]18         setNameWithFormat:@"%@ -rac_signalForControlEvents: %lx", RACDescription(self), (unsigned long)controlEvents];19 }

We can see that a RACSignal signal is created and a listener is added to the button when the block callback is executed. This method returns a RACSignal signal. At the same time, we call subscribeNext to subscribe to this signal. When we click the button and call the sendNext method to send the value, the subscribeNext block is called back.

2. Bind The textView listener (using textfield in the same way)
1 [self. myTextView. rac_textSignal subscribeNext: ^ (id x) {2 NSLog (@ "output: % @", x); 3}];
3. Bind a gesture:
1 UITapGestureRecognizer * tap = [UITapGestureRecognizer new]; 2 [self. redView addGestureRecognizer: tap]; 3 [tap. rac_gestureSignal subscribeNext: ^ (id x) {4 NSLog (@ "click the red view"); 5}];

It can be seen that some common UI controls are relatively simple to use based on ReactiveCocoa. Here it is worth mentioning that when the UI control monitors the response process in proxy mode. For example, UIImagePicker. The following code implements a simple small function. click the button to select an image. After the image is selected, it is displayed on UIImageView.

4. Replace the proxy callback of the UI control:
1 [[self. btn rac_signalForControlEvents :( UIControlEventTouchUpInside)] subscribeNext: ^ (id x) {2 3 // click the button to bring up UIImagePicker 4 self. imagePicker = [UIImagePickerController new]; 5 [self. imagePicker. rac_imageSelectedSignal subscribeNext: ^ (id x) {6 // This block callback calls 7 NSLog (@ "% @", x) when the photo selection is complete ); 8 NSDictionary * dic = (NSDictionary *) x; 9 self. myImageView. image = dic [@ "UIImagePickerControllerOriginalImage"]; 10 [self. imagePicker dismissViewControllerAnimated: YES completion: nil]; 11}]; 12 // rac_delegateproxy is the proxy attribute under RAC. This line of code can be understood, the proxy under RAC will execute the block callback to replace the previous proxy to execute the imagePickerControllerDidCancel method.
13 [[self. imagePicker. rac_delegateProxy signalForSelector: @ selector (imagePickerControllerDidCancel :)] subscribeNext: ^ (id x ){
14 // when this block is called: When delegate needs to execute imagePickerControllerDidCancel
15 [self. imagePicker dismissViewControllerAnimated: YES completion: nil];
16}];
17
18 [self presentViewController: self. imagePicker animated: YES completion: nil]; 19 20 21}];
5. notifications under RAC:

Registration notification on the first page:

1 [[[nsicationicationcenter defacenter center] rac_addObserverForName: @ "ChangeColor" object: nil] subscribeNext: ^ (id x) {2 NSNotification * notification = (NSNotification *) x; 3 NSLog (@ "notification received: % @", notification. object); 4 self. view. backgroundColor = (UIColor *) notification. object; 5}];

The return button on the second page sends a notification:

[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeColor" object:[UIColor grayColor]];
6. Observer Design Mode Under RAC:

CurrentValue is an Int type attribute owned by the View Controller.

[[Self rac_valuesAndChangesForKeyPath: @ "currentValue" options :( NSKeyValueObservingOptionNew) observer: self] subscribeNext: ^ (id x) {// unpack the tuples, the values in the tuples are assigned to the variables in order. RACTupleUnpack (NSString * kind, NSString * new) = x; NSLog (@ "the value of currentValue is changed, the current value is equal to % @, % @ ", kind, new) ;}];

Click the button to change the value of currentValue

[[self.valueButton rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(id x) {        self.currentValue ++;    }];
Source code: https://github.com/SZT0728/ReactiveCocoaProgram

We can see that the UI under RAC is highly consistent with multiple Event Response block callback methods. The article is not the essence. I just want to write down my learning and share it in the simplest form. If any, please point out.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.