IOS, Objective-C chain programming

My last article “iOS program when processing flash back a code fix” intercept exceptions mentions “chain programming”, may have readers about “chain programming” will have some questions. On the “chain programming” is also available online this article here simply talk about some personal opinion, there is something wrong, please tell me what you are.
read the code

UIBtn (@ BTN1).Frame_l (CGRectMake (100, 100, 100, 60) (.BackgroundColor_l) [UIColor redColor].Action (self), @selector (action1:), UIControlEventTouchUpInside.AddToView (self.view));

The code will be more lines of code. Through the link into a code, which is one of the advantages of chain programming, programming is characteristic of chain. It is very simple and easy to use. How
can achieve this effect? I think a lot of people will think of grammar, in OC we will first think of a class instance of the object attributes can be specified. So how can we keep going? That is the constant return of the instance object. Continue to think, what is the return value? Method has a return value, but most of the methods can not be used! How to do. Yes, ah and Block, and Block can also declare the class attribute.
analysis to it, then we have a basic idea: attribute in the class with Block, and the return value of Block is an instance of the class, then we do some corresponding operation in the Block. The following
I simply said that in the case of UIButton. First of all, we create a UIButton Category, of course, can also be inherited. Maybe some small partners can not find the place, the last map. After all should be.

IOS, Objective-C chain programming

The next on (a piece of code to it, I think this is intuitive, later said, the first to separate).H

#import < UIKit/UIKit.h> @interface; UIButton (Link) @property (nonatomic, copy) UIButton * (^frame_l) (CGRect rect); @property (nonatomic, copy) UIButton * (^backgroundColor_l) (UIColor *color); @property (nonatomic, copy) UIButton * (^addToView) (UIView *view); @property (nonatomic, copy) UIButton * (^action) (ID target, SEL selector, UIControlEvents events); UIButton * UIBtn (ID name @end);
  • Here are four Block, which are used to set up frame, backgroundColor, to add to the parent view, and Action click event
  • The following C method, is used to create an instance of the object, which is equivalent to the parameters of the object name, do not have such [UIButton buttonWithType:UIButtonTypeCustom]; create, write more and come back to.M #import UIButton+Link.h UIButton *button; @implementation UIButton (Link) – (UIButton (* ^ (CGRect)) {return (CGRect ^ frame_l) FRA) {button.frame = FRA; return button;};} – (void) setFrame_l: (UIButton * (^) (CGRect) – frame_l (UIButton) {}; * (^) (UIColor * backgroundColor_l)) ^ {return (UIColor *color) {button.backgroundColor = color; return button;};} – (void setBackgroundColor_l: (UIButton) * (^) (UIColor *) backgroundColor_l – (UIButton) {}; * (^) (UIView * addToView)) ^ {return (UIView *view) {[view addSubview:button]; Return button;};} – (void) setAddToView: (UIButton * (^) (UIView *) addToView – (UIButton) {}; * (^) (ID, SEL, UIControlEvents) {return (ID) action ^ target, SEL selector, UIControlEvents events) addTarget: target action:selector {[button forControlEvents:events]; return button;};} – (void) setAction: (UIButton * (^) (ID, SEL, UIControlEvents) action @end UIButton) {}; * UIBtn (ID name) {button = [UIButton buttonWithType:UIButtonTypeCustom; return button;}
  • .m is mainly to achieve the properties of the getter and setter method, where the realization of setter is to eliminate the warning
  • In the C method, we initialize the global button object, and then the getter method for each property returns the object

That we use, reached the beginning of the article that the effect of the code, to achieve a “chain programming”. You can have little point
but now Xcode to Block that is not too friendly

IOS, Objective-C chain programming

When we press enter, only addToView will not automatically fill the following parameters

Said so much, interested in children’s shoes can continue to add property.

Finally, an egg, about Block

  • The call to Block we will first determine whether it is the nil, to prevent the collapse, it is often the if (block) {block} (); but you can do that! Block?: block (); time, the wrong place also please leave a message. Technology endless, common progress, committed to writing elegant code, yeah!