ReactiveCocoa (bottom), ReactiveCocoa (

Source: Internet
Author: User

ReactiveCocoa (bottom), ReactiveCocoa (

1. Bind Method

  • The core method for ReactiveCocoa operations isbind(Binding), and the core development method in RAC is alsoBind, The previous development method isAssignmentWith RAC development, we should focus on binding, that is, when creating an object, we can bind what we want to do in the future, instead of doing things after assigning values.

  • Column for example: the data is displayed on the control. Previously, the setModel method of the control is overwritten. With RAC, You can bind the data when creating the control.

  • The bind method is rarely used in development. bind is the underlying method in RAC. RAC has encapsulated many other useful methods. The underlying method is to call bind, which is easier to use than bind.

 

Method 1:

1 // listen to the content of the text box and splice the text "output:" 2 self. textField = [[UITextField alloc] initWithFrame: CGRectMake (20, 65,300,100)]; 3 self. textField. backgroundColor = [UIColor purpleColor]; 4 [self. view addSubview: self. textField]; 5 // concatenate 6 [self. textField. rac_textSignal subscribeNext: ^ (id x) {7 NSLog (@ "output: % @", x); 8}];

Input: Hello world in textField

Result:

 

Method 2: splice the returned results and use the bind method in RAC for processing.

A. bind method parameters: You need to input a block parameter whose return value is RACStreamBindBlock.

B. RACStreamBindBlock is a block type. The return value is a signal and the parameter (value, stop). Therefore, the block return value of the parameter is also a block.

 
1 // listen to the content of the text box and splice the text "output:" 2 self. textField = [[UITextField alloc] initWithFrame: CGRectMake (20, 65,300,100)]; 3 self. textField. backgroundColor = [UIColor purpleColor]; 4 [self. view addSubview: self. textField]; 5 [[self. textField. rac_textSignal bind: ^ RACSignalBindBlock _ Nonnull {6 7 // This block calls 8 return ^ RACSignal * (id value, BOOL * stop) as long as the source signal sends data) {9 // block Function: process the returned value. 10 // process the returned value and return the value through the signal. 11 // return signal. nil cannot be passed. If you really want to return an empty signal, use empty (actually an alloc init ). if empty is returned, the subscription chain is disconnected. return [RACSignal empty]; 12 return [RACReturnSignal return: [NSString stringWithFormat: @ "output: % @", value]; 13}; 14}] subscribeNext: ^ (id x) {15 NSLog (@ "subscribed location, that is, the signal processed: % @", x); 16}];

The output is as follows:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.