Efficient ios development-related to blocks

Source: Internet
Author: User

1. Replace delegate. If we have two viewcontrollers, a and B, some events are triggered on B after we push them from interface a to interface B, these times will affect the content on interface. There are two interfaces above. When we click button 3 on interface B, we hope that the text on interface a will change accordingly. In general, we will use delegate to implement it. The agent is powerful, but the agent is complicated in the event logic of the program. Let's see how to deal with block. The definition of a block statement is similar to a C function. It has a return value and 0 to multiple parameters. Next, the attribute declaration is similar to the general attribute declaration:

 typedef  (^CallBack)(  @property(nonatomic, copy)CallBack callBack;
Then, when the button event is triggered in B, block is called. Before calling, it is best to check whether it is nil.
 1 - (IBAction)click2:(id)sender 2 { 3     if(self.callBack) 4         self.callBack(2); 5 } 6  7  8 - (IBAction)click3:(id)sender 9 {10     if(self.callBack)11         self.callBack(3);12 }

 

Now the call is complete. Where is the specific implementation? Let's go back to a and execute the jump interface in a, and add the relevant implementation.
     SecondViewController *secondController = [[SecondViewController alloc]initWithNibName:          secondController.callBack = ^(          self.clickBtnLabel.text = [NSString stringWithFormat:           [self.navigationController pushViewController:secondController animated:YES];
When using block, pay attention to the ownership issue and be careful to generate a loop, which will lead to the release of resources. For example, if a pointer is used in a block and the pointer is the owner of the block, a circular reference will be generated. They are both strongly referenced and cannot be released. To avoid similar problems, _ weak must be used to mark the block owner. Delegate and block are used in the preceding scenario. If there are many methods in delegate of a class, put them in delegate. 2. The system-defined blockios contains a large number of blocks that have been defined by the system. Using these blocks to implement specific functions can make the overall code more concise and efficient. For example, when traversing the dictionary, ios provides:
- ()enumerateKeysAndObjectsUsingBlock:( (^)( key,  obj, BOOL *stop))block;

For example, we need to find a value based on the key in the dictionary and record the value.

 

     NSArray *keyArray = @[, , , , ,      NSArray *valueArray = @[, , , , ,      NSDictionary *enumDict =     __block NSString *valueString =     [enumDict enumerateKeysAndObjectsUsingBlock:^( key,  obj, BOOL *         ([key isEqualToString:              valueString =             *stop =      }];

The entire process is simplified.

In addition, when implementing UIView animation, block allows us to implement many special effects more easily and efficiently.

     [UIView animateWithDuration: animations:^         animateView.alpha =      } completion:^         animateView.alpha =      }];

This code changes the view transparency from 1.0 to completely transparent. It takes 0.5 seconds to re-display the view after the animation ends.

 

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.