1, replacing the delegate
If we have two viewController, a and b, with a screen when we push to b ', the number of events triggered b above, these times would affect the contents of a screen.
The above is two interfaces, when we click the button 3 b interface, I want a text interface is also changed accordingly
As
Under normal circumstances we will use the delegate to achieve.
agent is very powerful, but it is a procedural agent event logic also become complicated.
look at the how to deal with the block.
a block definition statement similar to the C function that returns a value, zero or more parameters, then the property declaration, with a general statement about the properties:
1 typedef void (^CallBack)(int index);
2
3 @property(nonatomic, copy)CallBack callBack;
Then b which is called when the trigger button event block, call a good idea to check the whether 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 }
Call now complete, the specific implementation in which it, we were back to a, in a place where the interface to perform jumps, plus the associated implementation.
1 SecondViewController *secondController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
2
3 secondController.callBack = ^(int index)
4 {
5 self.clickBtnLabel.text = [NSString stringWithFormat:@"click %d", index];
6 };
7
8 [self.navigationController pushViewController:secondController animated:YES];
When the block is, pay attention to the issue of ownership, be careful to generate cycle, that would cause the resource can not be released.
For example, in the use of a block pointer, the pointer is block happens to the owner, it will produce a circular reference, they are all strong references, can not be released. To avoid similar problems, you need to use __weak marked block owners. delegate and when the block is used in the above, depending on the context, if a class delegate, there are many ways, it is still on the delegate in achieving it.
2, the system-defined block
ios which there are a large system has been defined block, block implementation-specific use of these features, the overall code can be more concise and efficient.
For example, when traversing dictionary, ios it provides: This code implements the view
- (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key, id obj, BOOL *stop))block;
simple example, we want in the dictionary under the key to find a value, then the value recorded.
1 NSArray *keyArray = @[@"aa", @"ddd", @"cc", @"bb", @"ww", @"111"];
2 NSArray *valueArray = @[@"apple", @"ios", @"mac", @"xcode", @"view", @"array"];
3 NSDictionary *enumDict = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
4 __block NSString *valueString = nil;
5 [enumDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){
6 if([key isEqualToString:@"bb"])
7 {
8 valueString = obj;
9 *stop = YES;
10 }
11 }];
whole process becomes simple.
UIView animation as well as in implementation time, block allows us to achieve a lot more simple and efficient effects.
1 [UIView animateWithDuration:0.5 animations:^{
2 animateView.alpha = 0.0;
3 } completion:^(BOOL finished) {
4 animateView.alpha = 1.0;
5 }];
transparency changed from 1.0 to completely transparent with a time of 0.5 seconds, when the movie ends redisplay view.
没有评论:
发表评论