三种弹窗的解析

来源:互联网 发布:淘宝网上假发 编辑:程序博客网 时间:2024/05/16 06:02

1.第一种弹窗(分全系统和IOS8以上系统的两种做法)



UIAlertView

这种方法要添加上委托UIAlertViewDelegate

  1. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"提示内容" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

需要更多的按钮只需要在“确定”的后面添加 @"第三个按钮"依次类推即可。

一般写法

  1. -(void)autoupgrade
  2. {
  3.    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"版本更新" message:@"有新版本发布,是否立即升级?" delegate:self cancelButtonTitle:@"以后再说" otherButtonTitles:@"马上升级",@"取消", nil];
  4.    alert.tag = 1; //给定一个值方便在多个UIAlertView时能进行分辨
  5.    [alert show];//让弹窗显示出来
  6. }

委托的监听方法

  1. -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
  2. {
  3.    if(buttonIndex == 1){  
  4.        if(alertView.tag == 1){//tag就是专门拿来分辨的
  5.            //[[NSNotificationCenter defaultCenter] postNotificationName:@"theLeftDot" object:nil];
  6.            
  7.            NSLog(@"tag为1的alert");
  8.        }
  9.    }
  10. }
无论跳出来的弹窗是一个按键,还是几个按键,buttonIndex的值按实例时添加按钮的顺序从0逐渐加1.(以后再说,马上升级,取消)。

如果你按以上的做法xcode会提示你warning。理由是不赞成你用这些老方法提倡使用

UIAlertController

  1. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"版本更新" message:@"有新版本发布,是否立即升级?" preferredStyle:UIAlertControllerStyleAlert];
  2.    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){NSLog(@"");}];
  3.    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
  4.    
  5.    
  6.    [alertController addAction:cancelAction];
  7.    [alertController addAction:okAction];
  8.    [self presentViewController:alertController animated:YES completion:nil];

第二种(一般建议在重要的数据增删二次确认中使用)

  1. UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"更新提示" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"其他", nil];
  2.        
  3. [sheet showInView:self.view];
监听方法
  1. -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

第三种

通常用在点击下载或者无足轻重的消息提示上。

  1. UILabel *label = [[UILabel alloc] init];
  2.    label.text = [NSString stringWithFormat:@"成功下载"];
  3.    label.font = [UIFont systemFontOfSize:12];
  4.    label.textAlignment = NSTextAlignmentCenter;//文本居中
  5.    label.textColor = [UIColor whiteColor];
  6.    label.backgroundColor = [UIColor blackColor];
  7.    label.frame = CGRectMake(0, 0, 150, 25);
  8.    label.center = CGPointMake(160, 240);
  9.    //设置圆角,大多数圆角或者圆形提示弹窗都能利用这个修出来
  10.    label.layer.cornerRadius = 5;
  11.   //圆角的显示
  12.    label.clipsToBounds = YES;
  13. or
  14.   label.layer.masksToBounds = YES; //因为ios7之后默认为no
  15.    [self.view addSubview:label];

同时结合 alpha,动画,最后利用

  1. [label removeFromSuperview]
删除 就能做出一个漂亮的弹窗了



0 0