ios8之后的UIAlertController详解
来源:互联网 发布:论文改数据会被发现吗 编辑:程序博客网 时间:2024/05/12 17:58
昨天工程里需要将 UIActionSheet 中字体的颜色统一为系统的蓝色,所以便去网上找办法。
办法很简单,遍历出UIActionSheet中的子控件。当然 这是ios8之前的方法,为什么这么说呢,等下你就知道了,先说说ios8之前的调用吧。
IOS8之前
创建一个UIActionSheet
- UIActionSheet *t_actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@”取消” destructiveButtonTitle:@”拨号” otherButtonTitles:@”发送短信”, @”复制名片”, nil nil];
- t_actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
- [t_actionSheet showInView:self.view];
UIActionSheet *t_actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拨号" otherButtonTitles:@"发送短信", @"复制名片", nil]; t_actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque; [t_actionSheet showInView:self.view];
// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
- -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
- {
- if (buttonIndex == 0) {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@”tel://%@”,_mar_num[_selectRow][1]]]];
- }else if (buttonIndex == 1) {
- [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@”sms://%@”,_mar_num[_selectRow][1]]]];
- }else if(buttonIndex == 2) {
- UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
- pasteboard.string = [NSString stringWithFormat:@”%@:%@”,_mar_num[_selectRow][2],_mar_num[_selectRow][1]];
- }
- }
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex == 0) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",_mar_num[_selectRow][1]]]]; }else if (buttonIndex == 1) { [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@"sms://%@",_mar_num[_selectRow][1]]]]; }else if(buttonIndex == 2) { UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; pasteboard.string = [NSString stringWithFormat:@"%@:%@",_mar_num[_selectRow][2],_mar_num[_selectRow][1]]; }}
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet; // before animation and showing view
- - (void)willPresentActionSheet:(UIActionSheet *)actionSheet
- {
- for (id subViwe in actionSheet.subviews) {
- if ([subViwe isKindOfClass:[UIButton class]]) {
- UIButton *button = (UIButton*)subViwe;
- [button setTitleColor:[UIColor colorWithRed:0 green:0.478431 blue:1 alpha:1] forState:UIControlStateNormal];
- }
- }
- }
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet{ for (id subViwe in actionSheet.subviews) { if ([subViwe isKindOfClass:[UIButton class]]) { UIButton *button = (UIButton*)subViwe; [button setTitleColor:[UIColor colorWithRed:0 green:0.478431 blue:1 alpha:1] forState:UIControlStateNormal]; } }}
赋上断点后的截图,
你可以清晰的看到,在即将出现的UIActionSheet 里面是有5个子视图的,其中4个按钮都为我自己添加,(第0个可能是背景颜色吧、没有去试过)然后你可以看代码,取出之后赋给一个 UIButton 再改变属性。
当你这样相等之后,两个按钮其实占有的是同一个指针,所以可以改变其中按钮的属性。当然你也可以直接用subView(UIAlertButton)改变属性。
这样之后,便可以改变字体中颜色了。
IOS8之后
重要:UIActionSheet在iOS 8不赞成。(注意,UIActionSheetdelegate也是过时的。)来创建和管理动作片在iOS 8之后,转而使用UIAlertController与preferredstyle的UIAlertControllerstyleactionsheet。
在应用程序的目标版本的iOS到iOS 8之前,使用UIActionSheet类向用户显示一组选择如何进行一个给定的任务。你也可以使用动作片提示用户确认潜在危险的行动。动作片包含一个可选的标题和一个或多个按钮,每一个对应于所采取的行动。
使用该类的属性和方法来配置动作片的消息,风格,和之前提交按钮。你也应该指定一个代表你的动作片。你的委托对象负责执行与任何按钮时,他们被窃听和应符合uiactionsheetdelegate协议的行动。有关实现委托的方法的更多信息,见UIActionSheetdelegate协议参考。
- UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
- [alertCtrl addAction:[UIAlertAction actionWithTitle:@”取消”
- style:UIAlertActionStyleCancel
- handler:^(UIAlertAction *action) {
- //code
- }]];
UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; [alertCtrl addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { //code }]];
[self presentViewController:alertCtrl animated:YES completion:nil];
或者你可以- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
- // Create the actions.
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
- NSLog(@”The \”Okay/Cancel\” alert’s cancel action occured.”);
- }];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; // Create the actions. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { NSLog(@"The \"Okay/Cancel\" alert's cancel action occured."); }];
- [alertController addAction:cancelAction];
- [self presentViewController:alertController animated:YES completion:nil];
[alertController addAction:cancelAction]; [self presentViewController:alertController animated:YES completion:nil];
其中,几个属性不用说你也应该知道
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,
UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
UIAlertActionStyleDefault = 0,
UIAlertActionStyleCancel,
UIAlertActionStyleDestructive
} NS_ENUM_AVAILABLE_IOS(8_0);
在这里,如何修改颜色字体呢 我想可能是 UIAlertController 的这个方法吧。。
- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;
在这里 我直接用了UIAlertActionStyleDestructive 把其中的某项改成UIAlert的红色,所以没有真正自定义颜色,不过去了解一下估计就可以了,还是挺简单的。
- ios8之后的UIAlertController详解
- ios8之后的UIAlertController
- iOS8.0之后的UIAlertController
- iOS8之后UIAlertController
- iOS8的UIAlertController
- iOS8之后UIAlertView被弃用 UIAlertController成功上位
- iOS9之后支付宝的回调以及iOS8之后提示框UIAlertController
- iOS8 UIAlertView 和 UIActionSheet 的新用法 UIAlertController详解
- IOS SDK详解之UIAlertController(IOS8之后替代AlertView和ActionSheet)
- IOS SDK详解之UIAlertController(IOS8之后替代AlertView和ActionSheet)
- IOS SDK详解之UIAlertController(IOS8之后替代AlertView和ActionSheet)
- IOS SDK详解之UIAlertController(IOS8之后替代AlertView和ActionSheet)
- iOS8开发之iOS8的UIAlertController
- iOS8开发之iOS8的UIAlertController
- UIAlertController在ios8的使用
- IOS8 UIAlertController
- iOS8之后的UIAlertViewController
- ios8.0后 UIAlertController的使用
- 在Android Studio中进行单元测试和UI测试
- java源码——java.lang.Object
- 面向切面编程的作用与实现简介
- git 命令总结
- 硅钢片之让步品
- ios8之后的UIAlertController详解
- POJ2003 RE到shi ,树存储的数据结构
- jsonp跨域请求资源使用方法与简单原理
- ASPAJAXExtSetup安装及配置
- c语言计算二维数组的行数、列数
- CentOS 6.6安装gcc4.8.2
- perl两个构建系统,Makefile.PL/Build.PL
- linux下socket connect 阻塞方式 阻塞时间控制
- MAVEN部署分析