IOS Development Reactivecocoa (Basic)

Source: Internet
Author: User

Objective

A while ago looking at masonry, the new third-party layout framework, began to learn about chained programming. And then slowly began to understand functional programming and responsive programming. In this concentrated programming thought, began to contact and study the framework of Reactivecocoa. In the blogs of many great gods , have a high degree of evaluation of the framework, all kinds of show themselves how flexible use of Reactivecocoa. Today is mainly the framework of some of their own study notes.

1.ReactiveCocoa Introduction

Reactivecocoa (RAC), a new framework for iOS and OS development, is open source for GitHub.

The Reactivecocoa combines several programming styles:

Objective

A while ago looking at masonry, the new third-party layout framework, began to learn about chained programming. And then slowly began to understand functional programming and responsive programming. In this concentrated programming thought, began to contact and study the framework of Reactivecocoa. In the blogs of many great gods , have a high degree of evaluation of the framework, all kinds of show themselves how flexible use of Reactivecocoa. Today is mainly the framework of some of their own study notes.

1.ReactiveCocoa Introduction

    • Reactivecocoa (RAC), a new framework for iOS and OS development, is open source for GitHub.

    • The Reactivecocoa combines several programming styles:

Reactivecocoa is described by developers as a responsive functional programming framework (FRP); using RAC does not need to consider the order of calls, directly considering the results, making the code highly aggregated and easy to manage.

2.ReactiveCocoa use

2.1 racsiganl (Core class of RAC): Signal class for data transfer

Note the point:

    • The signal is a cold signal by default, the value changes, and does not trigger the change signal, only subscribe to the signal, will trigger the change signal.

    • How to subscribe to the signal: Subscribenext:

//Create a signal

racsignal *singal = [racsignal createsignal: ^racdisposable *(ID <RACSubscriber> subscriber) {

//Send a signal.

[subscriber sendnext:@ "Ricky"];

//If the data is not sent, it is best to send the signal to complete, the internal will automatically call [racdisposable disposable] unsubscribe signal.

[subscriber sendcompleted];

return [racdisposable disposablewithblock: ^{

//Signal cancellation

}];

}];

//subscribe signal, activate signal

[singal subscribenext: ^(ID x) {

//x: The signal is sent to the data

NSLog(@ "%@",x);

}];

2.2 Replacing Target and action

    • TextField character changes

Uitextfield *textfield =[[uitextfield alloc]initwithframe: CGRectMake (+, +, + )];

TextField. BackgroundColor =[uicolor redcolor];

[self. View addsubview: TextField];

//Monitor input change of Uitextfield

[[TextField rac_textsignal] subscribenext: ^(ID x) {

NSLog(@ "%@",x);

}];

//x is the text of TextField, so change the ID to nsstring

[[TextField rac_textsignal] subscribenext:^ (NSString * text) {

//

NSLog (@ "%@", text);

//    }];

    • Monitoring of the events of UIButton

UIButton *button =[UIButton buttonwithtype: Uibuttontypecustom];

button. frame = CGRectMake (+, +, + );

button. BackgroundColor = [uicolor yellowcolor];

[self. View addsubview: Button];

[button rac_signalforcontrolevents: UIControlEventTouchUpInside] subscribenext : ^(ID x) {

//This x is the button, already know the type can change the ID x to UIButton *button

NSLog(@ "%@",x);

}];

    • Gesture Events

The RAC notification does not need to be removed, and the RAC handles the removal of the change notification itself

[[[nsnotificationcenter defaultcenter] rac_addobserverforname: Uiapplicationdidenterbackgroundnotification Object: nil] subscribenext: ^(ID x) {

NSLog(@ "%@",x);

}];

    • Timer

Delay a certain amount of time to do sth.

[[racscheduler mainthreadscheduler]afterdelay:2 Schedule: ^{

NSLog(@ "Ricky");

}];

How often each event is called

[ [racsignal interval:1 onscheduler:[racscheduler Mainthreadscheduler]] subscribenext: ^(ID x) {

NSLog(@ "%@",x);

}];

    • KVO

      Monitor Uiscrollview's Contentoffset

Uiscrollview *scroller =[[uiscrollview alloc]initwithframe: CGRectMake (+, +, + )];

[self. View addsubview: scroller];

[racobserve(scroller, contentoffset) subscribenext: ^( ID x) {

NSLog(@ "%@",x);

}];

IOS Development Reactivecocoa (Basic)

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.