2013-11-09 4 views
0

Я пытаюсь закрыть 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 завершен, затем отображаются выбранные изменения. ... это так странно.

всякая помощь была бы принята с благодарностью.

UPDATE: Это метод, который я вызываю, есть элемент 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; 

    } 
} 
+0

Вы использовали многопоточность внутри 'displaySelected'? – Roshan

+0

Нет, я не уверен, как это сделать. – HurkNburkS

+0

Тогда вы в порядке ... можете ли вы отправить код с 'displaySelected'? – Roshan

ответ

1

Вам необходимо создать 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]]; 
} 

Это упрощение, но должно дать вам представление о том, как действовать.

+0

Я обновил свой код .. однако я получаю эту ошибку ** Несовместимые типы указателей блоков, отправляющие 'void (^) (BOOL)' в параметр типа 'void (^)()' ** – HurkNburkS

+0

К сожалению, небольшая опечатка. см. править. –

+0

нормально проверяя его – HurkNburkS

Смежные вопросы