美文网首页
iOS自动消失提示框

iOS自动消失提示框

作者: 十一遥 | 来源:发表于2017-04-19 22:52 被阅读480次

    方法一: (AlertView 苹果不建议使用了,好像是iOS9过期的)

    //第一种方法
    - (void)showDismissWithTitle:(NSString *)title  message:(NSString *)message durationTime:(NSTimeInterval)durationTime parent:(UIViewController *)parentController Finished:(void (^)(void))finished{
    
         UIAlertView * alert = [[UIAlertViewalloc]initWithTitle: title message: message delegate:self cancelButtonTitle:nil otherButtonTitles:nil,nil];
            
         [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:alert repeats:YES];
            
         [alert show];
    
    }
    
    - (void)timerAction:(NSTimer*)timer
    {
        UIAlertView * alert = (UIAlertView*)[timeruserInfo];
    
        [alert dismissWithClickedButtonIndex:0 animated:NO];
    
         alert = NULL;
    }
    
    //调用方法
     [self showDismissWithTitle:@"" message:@"收藏成功" durationTime:0.5 parent:self Finished:nil];
    
    

    方法二: (方法一的简版)

    /*
     *这个是可以自动消失的提提醒框
     *title 为 nil 则message 字体是粗体  title为@"" 则message字体为细体
     *durationTime alert持续时间, 建议写 1秒就好  0.5秒也可以 (若文字短 0.5较好)
     *例:[AGToolManager showDismissWithTitle:@"" message:@"收藏成功" durationTime:0.5 parent:self Finished:nil];
     */
    
    + (void)showDismissWithTitle:(NSString *)title  message:(NSString *)message durationTime:(NSTimeInterval)durationTime parent:(UIViewController *)parentController Finished:(void (^)(void))finished{
        
        UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
        
        [alerView show];
        
        [alerView performSelector:@selector(dismissWithClickedButtonIndex:animated:) withObject:@[@0, @1] afterDelay:durationTime];
    }
    
    //调用方法 (这个是封装的类方法)
    
        [AGToolManager showDismissWithTitle:@"" message:@"收藏成功" durationTime:0.5 parent:self Finished:nil];
    

    方法三 :(AlertController 苹果建议使用)

    - (void)showDismissWithTitle:(NSString *)title  message:(NSString *)message parent:(UIViewController *)parentController {
    
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
        [self presentViewController:alert animated:YES completion:nil];
    
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(creatAlert:) userInfo:alert repeats:NO];
        
    }
    
    - (void)creatAlert:(NSTimer *)timer{
    
        UIAlertController * alert = (UIAlertController *)[timer userInfo];
    
        [alert dismissViewControllerAnimated:YES completion:nil];
    
        alert = nil;
    
    }
    
    
    //调用方法
    
        [self showDismissWithTitle:@"" message:@"收藏成功" durationTime:0.5 parent:self Finished:nil];
    
    

    方法四:(HUD弹出窗口)

    + (void)showMsg:(NSString *)msg inView:(UIView*)theView{
        
        E_HUDView *alert = [[E_HUDView alloc] initWithMsg:msg];
    
        if (!theView){
            [[self getUnhiddenFrontWindowOfApplication] addSubview:alert];
        }
        else{
            [[E_HUDView getWindow] addSubview:alert];
        }
    
        [alert showAlert];
    }
    
    //调用方法
    
        [E_HUDView showMsg:@"加入收藏" inView:self.view];
    
    

    相关文章

      网友评论

          本文标题:iOS自动消失提示框

          本文链接:https://www.haomeiwen.com/subject/wtnhzttx.html