uialertview 图片_iOS系统弹框:UIAlertView,UIActionSheet, UIAlertController

在系统弹框中UIAlertView是一个比较古老的类,现在已经被废弃,但是还能使用.我们看一下效果就行.

UIAlertView.以及对应结果;(过期)

这个效果会出现在屏幕中间.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"人民币" message:@"6.65" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"A",@"B", nil];

alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;

[alertView show];

}

效果图如下:

b460ebd88e91

UIAlertView

UIActionSheet 以及对应结果;(过期)

这是一个在底部的效果,弹窗会在手机屏幕底部显示:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

//destructive 破坏,爆炸: 描述按钮 ,警告

UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"操作提示" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"中奖了" otherButtonTitles:@"A",@"B", nil];

[sheet showInView:self.view];

}

效果图如下:

b460ebd88e91

UIActionSheet

UIActionSheet和UIAlertView在点击选项的时候,可以调用它的代理,还处理点击事件.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 9_0);

UIAlertController

但是是毕竟已经过期了,不能再使用了,我们还是把重点放在新的弹框UIAlertController上吧,这是一个新的类,在iOS8.0之后才有的一个类,如果你的项目还在兼容古老的iOS7,那就要做一个判断了,在iOS7上不支持这个类,会crash掉的.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"标题" message:@"内容" preferredStyle:UIAlertControllerStyleActionSheet];

//默认只有标题 没有操作的按钮:添加操作的按钮 UIAlertAction

UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

NSLog(@"取消");

}];

//添加确定

UIAlertAction *sureBtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

NSLog(@"确定");

}];

//设置`确定`按钮的颜色

[sureBtn setValue:[UIColor redColor] forKey:@"titleTextColor"];

//将action添加到控制器

[alertVc addAction:cancelBtn];

[alertVc addAction :sureBtn];

//展示

[self presentViewController:alertVc animated:YES completion:nil];

}

b460ebd88e91

UIAlertController图1

b460ebd88e91

UIAlertController图2

在创建UIAlertController的时候,最后的UIAlertControllerStyle参数是一个枚举,UIAlertControllerStyleActionSheet表示在中间弹出弹框,UIAlertControllerStyleAlert表示在屏幕底部弹出弹框.

我们再给UIAlertController添加UIAlertAction的时候,UIAlertController会有一个Block会有一个回调,可以把,该UIAlertAction的点击事件写在这里面.

特殊的弹窗

在有的时候我们需要在项目中的弹框里加一个输入框,用来输入验证码或者电话号码等等.

可以用系统的方法,添加一个或多个输入框:addTextFieldWithConfigurationHandler,这个方法也有一个Block回调,但里面可以拿到添加的TextField,然后可以对这个TextField进行一些设置,比如添加placeholder,设置字体大小,等等.

//添加确定

UIAlertAction *sureBtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

UITextField *txt = [alertVc.textFields objectAtIndex:0];

NSLog(@"%@",txt.text);

}];

[alertVc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

textField.placeholder = @"请输入账号";

}];

[alertVc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

textField.placeholder = @"请输入密码";

}];

b460ebd88e91

带输入框的UIAlertController

文本设置

这些基本能满足我们日常需要的,但是个别的的时候,我们再显示的时候需要对弹窗内容进行简单的设置,比如:行间距,文字颜色,等等.

NSString *content = @"在这里写上需要弹出显示并需要做格式处理的文本内容";

NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString: content];

NSInteger length = [content length];

[alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:kColorHex(0x030303) range:NSMakeRange(0, length)];

[alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, length)];

[alertVc setValue:alertControllerMessageStr forKey:@"attributedMessage"];

这里只是做一个简单的处理,把文本转化为富文本,富文本可以进行各种各样的设置.

富文本设置可以参考文章:iOS 富文本使用,

如何把文章排版变得更好看,点击:Markdown使用指南(常用语法,干货).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值