您的位置:首页 > 其它

三种弹窗的解析

2016-01-22 23:28 267 查看
1.第一种弹窗(分全系统和IOS8以上系统的两种做法)



UIAlertView
这种方法要添加上委托UIAlertViewDelegate

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

[/code]

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

-(void)autoupgrade

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"版本更新" message:@"有新版本发布,是否立即升级?" delegate:self cancelButtonTitle:@"以后再说" otherButtonTitles:@"马上升级",@"取消", nil];

alert.tag = 1; //给定一个值方便在多个UIAlertView时能进行分辨

[alert show];//让弹窗显示出来

}

[/code]

委托的监听方法

-(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");

}

}

}

[/code]

无论跳出来的弹窗是一个按键,还是几个按键,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];

[/code]

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



UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"更新提示" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"其他", nil];


[sheet showInView:self.view];

[/code]

监听方法

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

[/code]

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

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;

or

label.layer.masksToBounds = YES; //因为ios7之后默认为no

[self.view addSubview:label];

[/code]



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

[label removeFromSuperview]

[/code]

删除 就能做出一个漂亮的弹窗了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: