Block for iOS development and iosblock
Block for iOS development 1. What is block? The role of block
Common UI development and network functions implement callback. The button event processing method is the callback method, and the callback processing after the network download
(1) The target-action button is passed in by a method.
(2) input a pointer self in the table view to call back the method in the View Controller.
(3) block statement block, which solves the callback. It is understood as an "anonymous function" and defined in the method.
2. Basic use of block (syntax)
Knowledge points:
Define block variables and block statement Blocks
Block parameters and return values
Block capture external variables (including self)
// Block is interpreted as an anonymous function // void func () // {//} // 1. definition of block variables // tips: The syntax is strange // void func (); // defines the block variable, ^ indicates the definition of the block, // tip: The function name is enclosed by parentheses, add ^ void (^ block) (); // define the block statement block before the function name and store it in the block Variable. block = ^ void () {NSLog (@ "block") ;}; // execute block (); // 2. block // instance with parameters and return values to calculate the sum of the two blocks // int (^ MyAdd) (int x, int y ); // MyAdd = ^ int (int x, int y) // {// return x + y; //}; int (^ MyAdd) (int x, int y) = ^ int (int x, int y) {return x + y ;}; NSLog (@ "% d", MyAdd (); // 3. block: capture external variables // block precautions for using block external variables // int num = 10; _ block int val = 100; _ weak typeof (self) weakSelf = self; void (^ b1) () = ^ void () {// global variables can be modified _ page = 10; // unchangeable // num = 11; // The _ block variable can be changed to val = 101; // there may be a warning due to memory problems. Note: // _ weak typeof (self) weakSelf = self; // define weakSelf outside the block // weakSelf. url = @ "text"; // self. url = @ "text"; weakSelf. url = @ "text"; NSLog (@" 1% @ ", weakSelf. url) ;}; b1 ();
3. block application in Development (OC, UI, Network) (1) Use block for sorting
// // Dog *ahua = [[Dog alloc] init]; ahua.nickname = @"ahua"; ahua.age = 4; Dog *amiao = [[Dog alloc] init]; amiao.nickname = @"amiao"; amiao.age = 3; Dog *dahuang = [[Dog alloc] init]; dahuang.nickname = @"dahuang"; dahuang.age = 5; NSMutableArray *muArr = [[NSMutableArray alloc] initWithArray:@[ahua,amiao,dahuang]];// muArr sortUsingSelector:<#(SEL)#> [muArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { Dog *aDog = obj1; Dog *bDog = obj2;// return aDog.age>bDog.age; return [aDog.nickname compare:bDog.nickname]<0; }]; for(Dog *d in muArr) { NSLog(@"nickname:%@ age:%d",d.nickname,d.age); }
(2) Use block for animation
// 2. UIView animation UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (100,100,100,100)]; label. text = @ "I Am a label"; label. backgroundColor = [UIColor redColor]; [self. view addSubview: label]; // [UIView animateWithDuration: <# (NSTimeInterval) #> animations: <# ^ (void) animations #>]; [UIView animateWithDuration: 2 animations: ^ {CGRect frame = label. frame; frame. origin. y + = 200; label. frame = frame;} completion: ^ (BOOL finished) {NSLog (@ "Animation 1 ends"); [UIView animateWithDuration: 1 animations: ^ {label. transform = CGAffineTransformMakeRotation (M_PI);} completion: ^ (BOOL finished) {NSLog (@ "Animation 2 ends") ;}];}];
(3) Use block to implement interface value passing
If there are two interfaces, interface A and interface B, interface A creates interface B, and interface B is passed to interface.
Set block in the jump function on interface
-(Void) click :( UIButton *) but {SecondViewController * svc = [[SecondViewController alloc] init]; // set block [svc setChanhgeBackgeoundColor: ^ (NSString * color) {if ([color isEqualToString: @ "blue"]) {self. view. backgroundColor = [UIColor blueColor]; NSLog (@ "") ;}}]; [self presentViewController: svc animated: YES completion: nil];}
Save block on interface B
@ Interface SecondViewController: UIViewController // void action (NSString * color); // void (^ action) (NSString * color ); // to pass block-(void) setChanhgeBackgeoundColor :( void (^) (NSString * color) action to the second interface; @ end
Run the block passed by interface a on interface B.
// Create a category @ interface SecondViewController () {// defines a block type variable. To save the input parameter void (^ _ action) (NSString * color);} @ end
// Save the uploaded block as a global variable-(void) setChanhgeBackgeoundColor :( void (^) (NSString *) action {_ action = action ;}
// Execute block in the click return event. Because the block is saved by global variables, all blick can execute-(void) click :( UIButton *) anywhere in the viewcontrol *) but {// change the color of the main interface if (_ action) {_ action (@ "blue"); NSLog (@ "vc2 click to end");} [self dismissViewControllerAnimated: YES completion: nil];}
Click to download the complete code