如何从块中获取变量的值?

时间:2015-06-24 08:59:22

标签: ios objective-c

我实现了如下方法:

-(void)startAnimating:(MyCompletion)performMethod {
_displayViewForActivityIndicator = [[ProcessingIndicationView alloc] initWithFrame:CGRectMake(-self.view.frame.size.width, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:_displayViewForActivityIndicator];
LogDebug(@"Start the method");
[UIView animateWithDuration:1.5f
                      delay:0.0f
                    options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^{
                     //end position:
                     _displayViewForActivityIndicator.frame = self.view.frame;
                 }
                 completion:^(BOOL finished)
 {
     performMethod();
 }];

}

我使用了以下方法:

  - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender: (id)sender
 {
 BOOL ret = NO;
 NSString *userId = [ViewsDataManager sharedManager].userID;
 LogDebug(@"userID = %@", userId);
 if ((userId!=nil)&&(![userId isEqualToString:@""])) {
    __block bool a = NO;
    [self startAnimating:^{
        a = [self deleteUserService];
        LogDebug(@"a1 = %d", a);
    }];
    LogDebug(@"a2 = %d", a);
    if (a) {
        [ViewsDataManager sharedManager].userID = nil;
        LogDebug(@"Delete user successfully");
        ret = YES;
    }
    else{
        ret = NO;
    }
}
LogDebug(@"ret == %d",ret);
return ret;

}

变量a在一个块中设置,我想在块外使用它的值。我怎么能这样做?

1 个答案:

答案 0 :(得分:4)

您的代码没有意义。动画运行后将调用完成块(异步!),但在此之前您将访问该变量。您可能也希望将其放入完成块中。

相关问题