如何使用Objective-C

我浏览了各种文档,但是在以下情况下不知道如何应用完成处理程序。

//example function.
//default textView height 100.0f

-(void)getHeightResult{
  [self initHeight];
  NSLog(@"%@",[self setTextManager]);
}

-(NSString *)setTextManager{
   if(self.textView.frame.size.heigh != 100){
      return @"new Height";
   } else{
      return @"Default Height;
   }
}

-(void)initHeight{
   int result;
   for(int i = 0 ; i <= 20: i ++){
     result = result + i;
   }
   //result = 210
   self.textView.frame.size.height = result;
}

执行上述功能时,getHeightResult始终输出“默认高度”。 我该如何应用完成处理程序以将其作为initHeight中计算出的值从诸如此类的源中返回?

我们要求您提供答案以帮助您了解完成处理程序,而不是询问实际来源。

huangxi500 回答:如何使用Objective-C

completionHandler的代码,

   - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self getSize:^(bool status) {
        if (status == true) {
            // perofm your operioan
            NSLog(@"Done");
        }
    }];
}

typedef void (^CompletionBlock)(_Bool status);

-(void)getSize:(CompletionBlock)block{
    __block int count = 0;

    [self initWidth:^(bool status) {
        count++;
        if (count == 2) {
            block(true);
        }
    }];

    [self initHeight:^(bool status) {
        count++;
        if (count == 2) {
            block(true);
        }
    }];
}


-(void)initHeight:(CompletionBlock)block{
    int result;
    for(int i = 0 ; i <= 100; i ++) {
        result = result + i;
    }
    block(true);
}

-(void)initWidth:(CompletionBlock)block{
    int result;
    for(int i = 0 ; i <= 20; i ++){
        result = result + i;
    }
    block(true);
}
本文链接:https://www.f2er.com/3144340.html

大家都在问