How to write an Objective-C Completion Block

后端 未结 5 2055
萌比男神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: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
    

提交回复
热议问题