我试图通过调用它来关闭另一个方法中的一个方法的UIView,UIView可以很好地关闭,但要等到当前方法中的所有过程都完成后才能关闭。

我想知道是否有一种方法可以强制首先发生第一件事(即关闭UIviews),然后继续当前方法?

这就是我的方法

- (void)selectDeselectAllPressed:(UIButton*)button {
    int id = button.tag;
    [SVProgressHUD showWithStatus:@"Updating" maskType:SVProgressHUDMaskTypeGradient];
    [self displaySelected]; // removes current view so you can load hud will not be behind it

    if (id == 1) {
        [self selectAllD];
    } else if (id == 2) {
        [self deselectAllD];
    } else if (id == 3) {
        [self selectAllI];
    } else if (id == 4) {
        [self deselectAllI];
    }
}


如您所见,按下按钮时会调用此方法,我想让displaySelected方法在调用任何其他方法之前执行所需的操作吗?

目前,当我调试它时,displaySelected方法发生了什么事情,它被称为线程遍历,然后继续执行if语句,然后在if语句中的方法完成后,对displaySelected进行更改。

任何帮助将不胜感激。

更新:这是我正在调用的方法,有一个if语句检查视图是否为nil,然后加载该视图,否则将其卸载。在点n处加载了我在视图上的代码,因此当我从selectDeselectAllPressed调用它时,将其输入else如果是关闭视图的方法的一部分。.我已调试,并看到线程进入此点

- (void) displaySelected {

    if (allBackGroundView == nil) {

        // load view

        CGFloat fullHeight = [UIScreen mainScreen].bounds.size.height;
        CGFloat fullWidth = [UIScreen mainScreen].bounds.size.width;

        // draw background
        background = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, fullHeight, fullWidth)];
        background.backgroundColor = [UIColor blackColor];
        background.alpha = 0.6;
        [self.view addSubview:background];

        UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(displaySelectedI)];
        gestureRecognizer.cancelsTouchesInView = NO;
        [background addGestureRecognizer:gestureRecognizer];

        allBackGroundView = [[UIView alloc] initWithFrame:CGRectMake(762.0, 43.0, 250.0, 220.0)];
        allBackGroundView.backgroundColor = [UIColor lightGrayColor];
        allBackGroundView.layer.borderWidth = 0.5;
        allBackGroundView.layer.borderColor = [UIColor darkGrayColor].CGColor;
        [self.view addSubview:allBackGroundView];

        // set labeltitles
        selecteAllDFitLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 18.0, 200.0, 30.0)];
        selecteAllDFitLabel.backgroundColor = [UIColor clearColor];
        selecteAllDFitLabel.font = [UIFont boldSystemFontOfSize:16];
        selecteAllDFitLabel.textColor = [UIColor whiteColor];
        selecteAllDFitLabel.text = @"Select all Dfit:";
        [allBackGroundView addSubview:selecteAllDFitLabel];

        deselecteAllDFitLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 65.0, 200.0, 30.0)];
        deselecteAllDFitLabel.backgroundColor = [UIColor clearColor];
        deselecteAllDFitLabel.font = [UIFont boldSystemFontOfSize:16];
        deselecteAllDFitLabel.textColor = [UIColor whiteColor];
        deselecteAllDFitLabel.text = @"Deselect all Dfit:";
        [allBackGroundView addSubview:deselecteAllDFitLabel];

        selecteAllILabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 120.0, 200.0, 30.0)];
        selecteAllILabel.backgroundColor = [UIColor clearColor];
        selecteAllILabel.font = [UIFont boldSystemFontOfSize:16];
        selecteAllILabel.textColor = [UIColor whiteColor];
        selecteAllILabel.text = @"Select all I:";
        [allBackGroundView addSubview:selecteAllILabel];

        deselecteAllILabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 170.0, 200.0, 30.0)];
        deselecteAllILabel.backgroundColor = [UIColor clearColor];
        deselecteAllILabel.font = [UIFont boldSystemFontOfSize:16];
        deselecteAllILabel.textColor = [UIColor whiteColor];
        deselecteAllILabel.text = @"Deselect all I:";
        [allBackGroundView addSubview:deselecteAllILabel];

        UIImage* okImage = [UIImage imageNamed:@"OK.png"];
        UIImage* noImage = [UIImage imageNamed:@"No.png"];

        selecteAllDFitButton =[UIButton buttonWithType:UIButtonTypeCustom];
        selecteAllDFitButton.tag = 1; // so I know what action to perform when i call selectDeselectAllPressed
        [selecteAllDFitButton addTarget:self
                       action:@selector(selectDeselectAllPressed:)
             forControlEvents:UIControlEventTouchUpInside];
        selecteAllDFitButton.frame = CGRectMake(200.0, 12.0, 40.0, 40.0);
        [selecteAllDFitButton setImage:okImage forState:UIControlStateNormal];
        [allBackGroundView addSubview:selecteAllDFitButton];

        deselecteAllDFitButton =[UIButton buttonWithType:UIButtonTypeCustom];
        deselecteAllDFitButton.tag = 2;
        [deselecteAllDFitButton addTarget:self
                                   action:@selector(selectDeselectAllPressed:)
                         forControlEvents:UIControlEventTouchUpInside];
        deselecteAllDFitButton.frame = CGRectMake(200.0, 62.0, 40.0, 40.0);
        [deselecteAllDFitButton setImage:noImage forState:UIControlStateNormal];
        [allBackGroundView addSubview:deselecteAllDFitButton];

        selecteAllIButton =[UIButton buttonWithType:UIButtonTypeCustom];
        selecteAllIButton.tag = 3;
        [selecteAllIButton addTarget:self
                                     action:@selector(selectDeselectAllPressed:)
                           forControlEvents:UIControlEventTouchUpInside];
        selecteAllIButton.frame = CGRectMake(200.0, 112.0, 40.0, 40.0);
        [selecteAllIButton setImage:okImage forState:UIControlStateNormal];
        [allBackGroundView addSubview:selecteAllIButton];

        deselecteAllIButton =[UIButton buttonWithType:UIButtonTypeCustom];
        deselecteAllIButton.tag = 4;
        [deselecteAllIButton addTarget:self
                                    action:@selector(selectDeselectAllPressed:)
                          forControlEvents:UIControlEventTouchUpInside];
        deselecteAllIButton.frame = CGRectMake(200.0, 162.0, 40.0, 40.0);
        [deselecteAllIButton setImage:noImage forState:UIControlStateNormal];
        [allBackGroundView addSubview:deselecteAllIButton];


    }
    else {
        [allBackGroundView removeFromSuperview];
        [background removeFromSuperview];
        allBackGroundView = nil;
        background = nil;

    }
}

最佳答案

您需要创建一个基于块的API,并将一个块传递给displaySelected

考虑:

- (void)selectDeselectAllPressed:(UIButton*)button {
    int id = button.tag;
    [SVProgressHUD showWithStatus:@"Updating" maskType:SVProgressHUDMaskTypeGradient];
    [self displaySelectedCompletionBlock: ^ (BOOL completed) {
        if(!completed) { return; }

        if (id == 1) {
            [self selectAllD];
        } else if (id == 2) {
            [self deselectAllD];
        } else if (id == 3) {
            [self selectAllI];
        } else if (id == 4) {
            [self deselectAllI];
        }
    }]; // removes current view so you can load hud will not be behind it
}


displaySelectedCompletionBlock:中:

- (void)displaySelectedCompletionBlock:(void(^)(BOOL))completionBlock
{
    //Setup animations.
    [UIView animateWithDuration:0.5 animations: ^ { /* Do animations here. */ } completion:[completionBlock copy]];
}


这是一个简化,但是应该使您知道如何进行。

10-07 15:45