Block of ios-design mode

Source: Internet
Author: User

Block is a block of code,

Block definition

return value (^ block name) (parameter 1, parameter 2 ...) );

You can use TypeDef to rename a block when you define it.

typedef void(^blockname) (nsstring *string);

Block and function similarity:

(1) Can save code

(2) There is a return value

(3) Tangible parameters

(4) Call the same way.

Specific implementation process:

//SecondViewController.h#import<UIKit/UIKit.h>typedefvoid(^blockname) (NSString *string);@interfaceSecondviewcontroller:uiviewcontroller@property (nonatomic, copy) Blockname Sendblock;@end//SECONDVIEWCONTROLLER.M-(Ibaction) Actiontow: (ID) Sender {Self.sendblock (self.textfile.text); [Self Dismissviewcontrolleranimated:yes completion:nil];}//VIEWCONTROLLER.M#import "ViewController.h"#import "SecondViewController.h"@interfaceViewcontroller () @property (weak, nonatomic) Iboutlet UILabel*lable, @property (Strong, nonatomic) Secondviewcontroller*SECONDVC;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.SELF.SECONDVC =[[Secondviewcontroller alloc]init];//How to resolve circular references in arc case: _weak__weak Viewcontroller *weakthis =Self ; Self.secondVC.sendBlock= ^ (NSString *string){//if you want to access the property, you also use __strong to decorate it. __strong Viewcontroller *strongthis =Weakthis; StrongThis.lable.text=string; };}-(Ibaction) Actionone: (IDsender {[self PresentViewController:self.secondVC animated:yes completion:nil];}

This article GitHub address Https://github.com/zhangkiwi/iOS_SN_Block

Block of ios-design mode

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.