How to write an Objective-C Completion Block

后端 未结 5 2052
萌比男神i
萌比男神i 2020-12-01 01:45

I\'m in a situation where need to call a class method from my view controller, have it do it\'s thing, but then perform some actions ONLY AFTER the class method has complete

相关标签:
5条回答
  • 2020-12-01 02:26

    You define the block as a custom type:

    typedef void (^ButtonCompletionBlock)(int buttonIndex);
    

    Then use it as an argument to a method:

    + (SomeButtonView*)buttonViewWithTitle:(NSString *)title 
                          cancelAction:(ButtonCompletionBlock)cancelBlock
                      completionAction:(ButtonCompletionBlock)completionBlock
    

    When calling this in code it is just like any other block:

    [SomeButtonView buttonViewWithTitle:@"Title"
                       cancelAction:^(int buttonIndex) {
                             NSLog(@"User cancelled");
                   } 
                     completionAction:^(int buttonIndex) {
                             NSLog(@"User tapped index %i", buttonIndex);
                   }];
    

    If it comes time to trigger the block, simply call completionBlock() (where completionBlock is the name of your local copy of the block

    0 讨论(0)
  • 2020-12-01 02:31

    I wrote a completionBlock for a class which will return the values of a dice after they have been shaked:

    1. Define typedef with returnType (.h above @interface declaration)

      typedef void (^CompleteDiceRolling)(NSInteger diceValue);
      
    2. Define a @property for the block (.h)

      @property (copy, nonatomic) CompleteDiceRolling completeDiceRolling;
      
    3. Define a method with finishBlock (.h)

      - (void)getDiceValueAfterSpin:(void (^)(NSInteger diceValue))finishBlock;
      
    4. Insert previous defined method in .m file and commit finishBlock to @property defined before

      - (void)getDiceValueAfterSpin:(void (^)(NSInteger diceValue))finishBlock{
          self.completeDiceRolling = finishBlock;
      }
      
    5. To trigger completionBlock pass predefined variableType to it (Don't forget to check whether the completionBlock exists)

      if( self.completeDiceRolling ){
          self.completeDiceRolling(self.dieValue);
      }
      
    0 讨论(0)
  • 2020-12-01 02:31

    Regarding to http://goshdarnblocksyntax.com/

    As a local variable:

    returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};
    

    As a property:

    @property (nonatomic, copy) returnType (^blockName)(parameterTypes);
    

    As a method parameter:

    - (void)someMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName;
    

    As an argument to a method call:

    [someObject someMethodThatTakesABlock:^returnType (parameters) {...}];
    

    As a typedef:

    typedef returnType (^TypeName)(parameterTypes);
    TypeName blockName = ^returnType(parameters) {...};
    
    0 讨论(0)
  • 2020-12-01 02:45

    I always use this when I want to write a block:

    http://fuckingblocksyntax.com

    EDIT

    If you are writing Swift then use this:

    http://fuckingswiftblocksyntax.com

    If the above links are not opening because of the obscene language, use this one.

    http://goshdarnblocksyntax.com/

    0 讨论(0)
  • 2020-12-01 02:47

    Simple Completion block

        // Completion block method
        -(void)myMethod:(void (^)(void))completion {
            NSLog(@"iPhone");
            completion();
            NSLog(@"iPod");
        }
    
       // Calling completion block method
        - (void)viewDidLoad {
            [super viewDidLoad];
    
            [self myMethod:^{
               NSLog(@"iPad");
            }];
        }
    
      // output
      iPhone
      iPad
      iPod
    
    0 讨论(0)
提交回复
热议问题