1.第一种弹窗(分全系统和IOS8以上系统的两种做法)
UIAlertView
这种方法要添加上委托UIAlertViewDelegate
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"提示内容" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
需要更多的按钮只需要在“确定”的后面添加 @"第三个按钮"依次类推即可。
一般写法
-(void)autoupgrade{UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"版本更新" message:@"有新版本发布,是否立即升级?" delegate:self cancelButtonTitle:@"以后再说" otherButtonTitles:@"马上升级",@"取消", nil];alert.tag = 1; //给定一个值方便在多个UIAlertView时能进行分辨[alert show];//让弹窗显示出来}
委托的监听方法
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{if(buttonIndex == 1){if(alertView.tag == 1){//tag就是专门拿来分辨的//[[NSNotificationCenter defaultCenter] postNotificationName:@"theLeftDot" object:nil];NSLog(@"tag为1的alert");}}}
无论跳出来的弹窗是一个按键,还是几个按键,buttonIndex的值按实例时添加按钮的顺序从0逐渐加1.(以后再说,马上升级,取消)。
如果你按以上的做法xcode会提示你warning。理由是不赞成你用这些老方法提倡使用
UIAlertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"版本更新" message:@"有新版本发布,是否立即升级?" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){NSLog(@"");}];UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];[alertController addAction:cancelAction];[alertController addAction:okAction];[self presentViewController:alertController animated:YES completion:nil];
第二种(一般建议在重要的数据增删二次确认中使用)
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"更新提示" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"其他", nil];[sheet showInView:self.view];
监听方法
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
第三种
通常用在点击下载或者无足轻重的消息提示上。
UILabel *label = [[UILabel alloc] init];label.text = [NSString stringWithFormat:@"成功下载"];label.font = [UIFont systemFontOfSize:12];label.textAlignment = NSTextAlignmentCenter;//文本居中label.textColor = [UIColor whiteColor];label.backgroundColor = [UIColor blackColor];label.frame = CGRectMake(0, 0, 150, 25);label.center = CGPointMake(160, 240);//设置圆角,大多数圆角或者圆形提示弹窗都能利用这个修出来label.layer.cornerRadius = 5;//圆角的显示label.clipsToBounds = YES;orlabel.layer.masksToBounds = YES; //因为ios7之后默认为no[self.view addSubview:label];
同时结合 alpha,动画,最后利用
[label removeFromSuperview]
删除 就能做出一个漂亮的弹窗了

本文介绍了iOS应用中三种常见的弹窗实现方式:UIAlertView、UIAlertController和UIActionSheet的使用方法及示例代码,并展示了如何通过自定义UIView创建简单的消息提示。
129

被折叠的 条评论
为什么被折叠?



