UIViewController Conditional Completion Argument Type Syntax

The syntax for void (^)(void) type of the termination argument, implemented by the UIViewController method:

 - (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion 

aroused my curiosity, and I could not find any documentation for him. Please can someone explain its purpose / meaning?

Thank you very much in advance.

+6
source share
2 answers

Here is a discussion of the blocks from my book:

http://www.apeth.com/iOSBook/ch03.html#_blocks

Here is an example, but here is an example that is closer to what you are asking:

 [self transitionFromViewController:fromvc toViewController:tovc duration:0.4 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:^(BOOL done){ [tovc didMoveToParentViewController:self]; [fromvc removeFromParentViewController]; }]; 

The completion block accepts one parameter, BOOL is called "done", but this is not used by its code. The idea is that the animation is executed, and then the code is executed in the completion block.

It is very important to be comfortable with the blocks, because they are the way of the future. For example, viewing animations in iOS 4 uses them, as described in the "Block Based Animation" section of my book (first read about the old method, then read about the new iOS 4 method):

http://www.apeth.com/iOSBook/ch17.html#_view_animation

In iOS 5, blocks are even more important; more and more situations where they are not optional.

Blocks are also a way to use GCD (grand central scheduling), which is far and far the best way to do multi-threading.

+12
source

This will be for the completion block. A block is a piece of code that can be represented as an argument (often considered, as here, to process terminations) in the API. There are many block functions, including the ability to reference memory and maintain state.

See the documentation for this increasingly popular Obj-C feature: http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/00_Introduction.html

Blocks provide what can be thought of as callback behavior (usually achieved by delegation or notification), but they allow the programmer to include completion logic in the same context as the initiating action, making the code more expressive and conveying the full progression of the intended behavior .
+3
source

Source: https://habr.com/ru/post/900849/


All Articles