1

je dois montrer différents types d'alertes avec un délai différent, afficher les alertes un par un.une alerte afficher et rester un peu de temps, puis la prochaine alerte montrent avec retard. ... un par un Ceci est mon codeComment afficher une par une alerte à différents temps et de retard

for (int j = 0; j < [[CMAppData appData].arrayDistanceAlert count]; j++) 
{ 

     CMBeaconsDisplayList * displayData = [[CMAppData appData].arrayDistanceAlert objectAtIndex:j]; 


     if ([displayData.type isEqualToString:@"1"]) 
     { 
      if ([displayData.type isEqualToString:@"1"] &&[displayData.displayForTime isEqualToString:@"0"]) { 


       [self performSelector:@selector(dismissAlertView:) withObject:displayData afterDelay:[displayData.displayForTime intValue]*60]; 
      } 

      else{ 

       double delayInSeconds =[displayData.displayForTime intValue] *10 ; 

       dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 
       dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 

        NSLog(@"%f",delayInSeconds); 

        UIAlertView * alert =[[UIAlertView alloc] initWithTitle:displayData.title message:displayData.content delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"pause", nil]; 
        alert.alertViewStyle =UIAlertViewStyleDefault; 
        [alert show]; 
        [self performSelector:@selector(dismissUIAlertViewType:) withObject:alert afterDelay:15]; 

       }); 

           } 

     } 

     else { 

      if ([displayData.type isEqualToString:@"2"]&&[displayData.displayForTime isEqualToString:@"0"]) { 

       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:displayData.title message:displayData.content delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; 
       [alertView show]; 
      } 
      else{ 
       double delayInSeconds =[displayData.displayForTime intValue]*10; 

       dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 
       dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 

        CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init]; 
        UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width-20, [UIScreen mainScreen].bounds.size.height-100)]; 
        customView.backgroundColor = [UIColor blueColor]; 
        [alertView setContainerView:customView]; 

        UIScrollView *MyScrollVw= [[UIScrollView alloc]initWithFrame:CGRectMake(0 ,0 ,[UIScreen mainScreen].bounds.size.width-20 ,[UIScreen mainScreen].bounds.size.height-100)]; 
        MyScrollVw.delegate= self; 
        [MyScrollVw setShowsHorizontalScrollIndicator:NO]; 
        [MyScrollVw setShowsVerticalScrollIndicator:YES]; 
        MyScrollVw.scrollEnabled= YES; 
        MyScrollVw.userInteractionEnabled= YES; 
        MyScrollVw.contentSize= CGSizeMake([UIScreen mainScreen].bounds.size.width-20 ,1150);//(width,height) 
        [customView addSubview:MyScrollVw]; 


        UILabel * lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, customView.frame.size.width, 30.0)]; 
        lblTitle.text = displayData.title; 
        lblTitle.font = [UIFont fontWithName:@"Arial" size:15.0]; 
        lblTitle.numberOfLines = 1; 
        lblTitle.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone 
        lblTitle.adjustsFontSizeToFitWidth = YES; 
        lblTitle.minimumScaleFactor = 10.0f/12.0f; 
        lblTitle.clipsToBounds = YES; 
        lblTitle.backgroundColor = [UIColor clearColor]; 
        lblTitle.textColor = [UIColor whiteColor]; 
        lblTitle.textAlignment = NSTextAlignmentCenter; 
        [MyScrollVw addSubview:lblTitle]; 

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, lblTitle.frame.origin.y+lblTitle.frame.size.height, customView.frame.size.width, 250)]; 
        displayData.image = [displayData.image stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 
        if(displayData.image && ![displayData.image isKindOfClass:[NSNull class]]) 
        { 
        [imageView sd_setImageWithURL:[NSURL URLWithString:displayData.image] placeholderImage:nil options:/* DISABLES CODE */ (0) == 0 ? SDWebImageRefreshCached : 0]; 
        } 
        if ([displayData.image length]==0) 
        { 
         [imageView setImage:[UIImage imageNamed:@""]]; 
        } 
        [MyScrollVw addSubview:imageView]; 


        NSString *videoString = displayData.video; 
        NSString *lastWord = [[videoString componentsSeparatedByString:@"="] lastObject]; 
        self.playerView = [[YTPlayerView alloc]init]; 
        self.playerView.frame = CGRectMake(0.0, imageView.frame.origin.y+imageView.frame.size.height, customView.frame.size.width, 250.0); 
        [self.playerView loadWithVideoId:lastWord playerVars:@{@"playsinline":@1}]; 
        [MyScrollVw addSubview:self.playerView]; 



        UIButton * btnPlayAudio = [[UIButton alloc] initWithFrame:CGRectMake(10.0,self.playerView .frame.origin.y+self.playerView .frame.size.height, customView.frame.size.width-20, 30)]; 

        [ btnPlayAudio addTarget:self action:@selector(playAudio:) forControlEvents:UIControlEventTouchUpInside]; 
        btnPlayAudio.backgroundColor = [UIColor redColor]; 
        [btnPlayAudio setTitle:@"Play Audio" forState:UIControlStateNormal]; 
        [btnPlayAudio setAccessibilityValue:displayData.audio]; 

        [MyScrollVw addSubview:btnPlayAudio]; 

        UILabel * lblDescription = [[UILabel alloc] initWithFrame:CGRectMake(10.0,btnPlayAudio .frame.origin.y + btnPlayAudio .frame.size.height, customView.frame.size.width-20, 100)]; 
        lblDescription.text = displayData.content; 
        lblDescription.font = [UIFont fontWithName:@"Arial" size:15.0]; 
        lblDescription.numberOfLines = 0; 
        [lblDescription sizeToFit]; 
        lblDescription.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone 
        lblDescription.adjustsFontSizeToFitWidth = YES; 
        lblDescription.minimumScaleFactor = 10.0f/12.0f; 
        lblDescription.clipsToBounds = YES; 
        lblDescription.backgroundColor = [UIColor clearColor]; 
        lblDescription.textColor = [UIColor whiteColor]; 
        lblDescription.textAlignment = NSTextAlignmentCenter; 
        [MyScrollVw addSubview:lblDescription]; 
        [alertView show]; 

        [self performSelector:@selector(dismissImageAlertView:) withObject:alertView afterDelay:15]; 

       }); 

       delayInSeconds = delayInSeconds * 10; 

      } 

     } 


    } 
+1

Configurez votre code pour la création d'alerte, en passant dans les valeurs dont vous avez besoin pour cette alerte spécifique, comme « createAlertWithText: @ « texte » » et ensuite se concentrer sur le bit de retard - travail sur la logique quand vous voulez l'alerte à tirer, que faire si une alerte est affichée et ainsi de suite. Gardez le «cerveau» et le «affichage» dans différentes méthodes. –

Répondre

-1

setTimeout: permet d'exécuter une fonction une fois après l'intervalle de temps.

alert('Before the Delay'); 

function afterDelay() { 
    alert('After the Delay'); 
} 

setTimeout(afterDelay, 2000);