What is the best way to check if a UIAlertController is already presenting?

后端 未结 13 2080
后悔当初
后悔当初 2020-12-04 17:26

I have a tableview which, when loaded, each cell could possibly return an NSError, which I have chosen to display in a UIAlertController. Problem is I get this error in the

13条回答
  •  感情败类
    2020-12-04 17:58

    I found I needed to create a queue to stack the UIAlertController requests.

    NSMutableArray *errorMessagesToShow; // in @interface
    errorMessagesToShow=[[NSMutableArray alloc] init];  // in init
    
    -(void)showError:(NSString *)theErrorMessage{
        if(theErrorMessage.length>0){
            [errorMessagesToShow addObject:theErrorMessage];
            [self showError1];
        }
    }
    -(void)showError1{
        NSString *theErrorMessage;
        if([errorMessagesToShow count]==0)return; // queue finished
    
        UIViewController* parentController =[[UIApplication sharedApplication]keyWindow].rootViewController;
        while( parentController.presentedViewController &&
          parentController != parentController.presentedViewController ){
            parentController = parentController.presentedViewController;
        }
        if([parentController isKindOfClass:[UIAlertController class]])return;  // busy
    
        // construct the alert using [errorMessagesToShow objectAtIndex:0]
        //  add to each UIAlertAction completionHandler [self showError1];
        //   then
    
        [errorMessagesToShow removeObjectAtIndex:0];
        [parentController presentViewController:alert animated:YES completion:nil]; 
    }
    

提交回复
热议问题