博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS UIAlertView和UIAlertController的使用
阅读量:4290 次
发布时间:2019-05-27

本文共 9389 字,大约阅读时间需要 31 分钟。

UIAlertController使用:

// 当cell被选中的时候,就会调用

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    

    // 取出数组中对应的model

    HeroModel *heroModel = self.dataArray[indexPath.row];

   

    // 弹出提示框

    UIAlertView *alertView = [[UIAlertViewalloc]

                              initWithTitle:@"编辑"

                                                        message:nil

                                                       delegate:self

                                              cancelButtonTitle:@"取消"

                                              otherButtonTitles:@"确定",nil];

    

    // 显示输入框

    /**

     UIAlertViewStyleDefault = 0,

     UIAlertViewStyleSecureTextInput,

     UIAlertViewStylePlainTextInput,

     UIAlertViewStyleLoginAndPasswordInput

     */

    alertView.alertViewStyle =UIAlertViewStylePlainTextInput;

    

    

    // 把英雄的名字赋值给 alertView中textField

    // 取出aletView的 textField

    UITextField *textField = [alertView textFieldAtIndex:0];

    

    textField.text = heroModel.name;

    

    // 点击cell的时候,把row设置给alertView的tag属性

    alertView.tag = indexPath.row;

    

    

    // 展示alertView

    [alertView show];

   

}

 

#pragma mark -

#pragma mark -  alertView的代理方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    

    if (buttonIndex == 1) { // 点击了确定按钮

        /**

         修改数据显示,修改英雄的名字

         */

        

        // 取出 alertView中的文本输入框

        UITextField *textField = [alertView textFieldAtIndex:0];

        

        // 修改过之后的名字

        NSString *newName = textField.text;

        

        // 修改数据源中 的英雄名字

        // 取出alertView 的tag ,点击的某个行

        NSInteger index = alertView.tag;

        

        // 取出点击行所对应的数据源中的模型

        HeroModel *model = self.dataArray[index];

        

        // 修改model中的 name属性

        model.name = newName;

        

        

        // 刷新tableView中的数据,对所有数据进行刷新

//        [_tableView reloadData];

        

        

//        // 对某一行cell的数据进行刷新

        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:indexinSection:0];

       [_tableViewreloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];

        

    }

}

 

 

 

 

======================================

 

// 编辑cell的标题以及UIAlertController的使用

//当cell 被选中的时候, 就会调用

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {

    // 弹出alertController

    UIAlertController *controller = [UIAlertControlleralertControllerWithTitle:@"编辑"message:nilpreferredStyle:UIAlertControllerStyleAlert];

    

    // 添加action-动作(按钮)

    UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

    

    // 添加到 alerController

    [controller addAction:cancelAction];

    

    // 添加确定按钮,点击确定按钮会执行block中的代码

    UIAlertAction *sureAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {

        

        // 1. 取出新的英雄名字

        // 1.1 取出alertController中 textField

        UITextField *textField = [controller textFields][0];

        

        NSString *newName = textField.text;

        

        // 2. 修改模型中的数据

        HeroModel *heroModel = self.dataArray[indexPath.row];

        heroModel.name = newName;

        

        // 3. 刷新数据源

//        [_tableView reloadData];

        [_tableViewreloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];

        

    }];

    

    // 取出点击cell对应的模型

    HeroModel *heroModel = self.dataArray[indexPath.row];

    

    // 添加输入框

    [controller addTextFieldWithConfigurationHandler:^(UITextField *_NonnulltextField) {

        

        // 给textField 设置文本

        textField.text = heroModel.name;

    }];

    

    // 添加确定按钮到 alertController

    [controller addAction:sureAction];

    

    [selfpresentViewController:controller animated:YEScompletion:nil];

   

}

-=================================

 

自定义UIAlertController

主要是使用kvc的方式来自定义UIAlertController的样式:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"内容" preferredStyle:UIAlertControllerStyleAlert];    // 使用富文本来改变alert的title字体大小和颜色    NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:@"这里是标题"];    [titleText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24] range:NSMakeRange(0, 2)];    [titleText addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];    [alert setValue:titleText forKey:@"attributedTitle"];    // 使用富文本来改变alert的message字体大小和颜色    // NSMakeRange(0, 2) 代表:从0位置开始 两个字符    NSMutableAttributedString *messageText = [[NSMutableAttributedString alloc] initWithString:@"这里是正文信息"];    [messageText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(0, 6)];    [messageText addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];    [messageText addAttribute:NSForegroundColorAttributeName value:[UIColor brownColor] range:NSMakeRange(3, 3)];    [alert setValue:messageText forKey:@"attributedMessage"];    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];    // 设置按钮背景图片    UIImage *accessoryImage = [[UIImage imageNamed:@"selectRDImag.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];    [cancelAction setValue:accessoryImage forKey:@"image"];    // 设置按钮的title颜色    [cancelAction setValue:[UIColor lightGrayColor] forKey:@"titleTextColor"];    // 设置按钮的title的对齐方式    [cancelAction setValue:[NSNumber numberWithInteger:NSTextAlignmentLeft] forKey:@"titleTextAlignment"];    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];    [alert addAction:okAction];    [alert addAction:cancelAction];    [self presentViewController:alert animated:YES completion:nil];

 

==============自定义alertcontroler方法一

#import <UIKit/UIKit.h>

 

@interface SCAlertAction : UIAlertAction

@property (nonatomic,strong)UIColor *textColor;/**< 按钮title字体颜色 */

@end

 

************************

#import "SCAlertAction.h"

#import <objc/runtime.h>

 

@implementation SCAlertAction

 

//按钮标题的字体颜色

-(void)setTextColor:(UIColor *)textColor

{

    _textColor = textColor;

    

    unsigned int count =0;

    Ivar *ivars = class_copyIvarList([UIAlertAction class], &count);

    for(int i =0;i < count;i ++){

        

        Ivar ivar = ivars[i];

        NSString *ivarName = [NSStringstringWithCString:ivar_getName(ivar)encoding:NSUTF8StringEncoding];

        

        if ([ivarName isEqualToString:@"_titleTextColor"]) {

            

            [self setValue:textColor forKey:@"titleTextColor"];

        }

    }

}

 

@end

***********************************

 

#import <UIKit/UIKit.h>

 

@interface SCAlertController : UIAlertController

@property (nonatomic,strong)UIColor *tintColor;/**< 统一按钮样式 不写系统默认的蓝色 */

@property (nonatomic,strong)UIColor *titleColor;/**< 标题的颜色 */

@property (nonatomic,strong)UIColor *messageColor;/**< 信息的颜色 */

 

@end

***********************

//用法就很简单了,和系统原生UIAlertController一样,只是把UI换成SC,当然你可以改成自己喜欢的,但是别忘了改完.

//

//SCAlertController *alertController = [SCAlertController alertControllerWithTitle:@"你确定要退出吗?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

//alertController.tintColor = [UIColor redColor]; //这里统一设置各个按钮的颜色都为红色.

//当然,你还可以自定义某一个按钮的颜色.比如下面的取消按钮

//alertController.titleColor = [UIColor redColor];

//

取消

//SCAlertAction *cancelAction = [SCAlertAction actionWithTitle:@"我不想退出" style:UIAlertActionStyleCancel handler:nil];

//

单独修改一个按钮的颜色

//cancelAction.textColor = [UIColor greenColor];

//[alertController addAction:cancelAction];

//

退出

//SCAlertAction *exitAction = [SCAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

//    

//}];

//[alertController addAction:exitAction];

//

//[self presentViewController:alertController animated:YES completion:nil];

//}

//

 

 

#import "SCAlertController.h"

#import "SCAlertAction.h"

#import <objc/runtime.h>

@interface SCAlertController ()

 

@end

 

@implementation SCAlertController

 

-(void)viewDidLoad

{

    [superviewDidLoad];

    

    unsigned int count =0;

    Ivar *ivars = class_copyIvarList([UIAlertController class], &count);

    for(int i =0;i < count;i ++){

        

        Ivar ivar = ivars[i];

        NSString *ivarName = [NSStringstringWithCString:ivar_getName(ivar)encoding:NSUTF8StringEncoding];

        

        //标题颜色

        if ([ivarName isEqualToString:@"_attributedTitle"] &&self.title &&self.titleColor) {

            

            NSMutableAttributedString *attr = [[NSMutableAttributedStringalloc]initWithString:self.titleattributes:@{

NSForegroundColorAttributeName:self.titleColor,NSFontAttributeName:[UIFontboldSystemFontOfSize:14.0]}];

            [self setValue:attr forKey:@"attributedTitle"];

        }

        

        //描述颜色

        if ([ivarName isEqualToString:@"_attributedMessage"] &&self.message &&self.messageColor) {

            

            NSMutableAttributedString *attr = [[NSMutableAttributedStringalloc]initWithString:self.messageattributes:@{

NSForegroundColorAttributeName:self.messageColor,NSFontAttributeName:[UIFontsystemFontOfSize:14.0]}];

            [self setValue:attr forKey:@"attributedMessage"];

        }

    }

    

    //按钮统一颜色

    if (self.tintColor) {

        for (SCAlertAction *action in self.actions) {

            if (!action.textColor) {

                action.textColor = self.tintColor;

            }

        }

    }

}

 

@end

 

 

=====================方法2

 

//1.标题和提示内容的文字设置

//代码如下:

修改title

//NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:@"提示"];

//[alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];

//[alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];

//[alertController setValue:alertControllerStr forKey:@"attributedTitle"];

//

修改message

//NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:@"提示内容"];

//[alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 4)];

//[alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 4)];

//[alertController setValue:alertControllerMessageStr forKey:@"attributedMessage"];

 

修改取消或确定按钮

//if (cancelAction valueForKey:@"titleTextColor") {

//    [cancelAction setValue:[UIColor redColor] forKey:@"titleTextColor"];

//}

//[cancelAction setValue:[UIImage imageNamed:@""] forKey: "image"];//给取消按钮设置图片

 

 

转载地址:http://eqlgi.baihongyu.com/

你可能感兴趣的文章
周鸿祎:很多程序员聪明,但我一看就知道他不会成功
查看>>
编译程序遇到问题 relocation R_X86_64_32 against `.rodata' can not be used when making a shared object;
查看>>
Const指针 、 指向const的指针 、引用、指针
查看>>
GDB调试命令
查看>>
常见数据类型的字节数
查看>>
gcc/g++ 编译常见问题
查看>>
【设计模式】 工厂模式实例 C++ , 利用工厂模式写一个计算器
查看>>
opencv
查看>>
【图像处理】 一、OSTU分割
查看>>
Android常用的框架有哪些
查看>>
SSL 证书创建与部署
查看>>
学习搭建人工智能环境遇到的问题
查看>>
自己编写的计算器
查看>>
视频物体检测
查看>>
Tomcat启动遇到的问题
查看>>
Gradle-gradlew
查看>>
grpc-整合gradle与代码生成
查看>>
grpc-gradle与grpc流程完美整合(3)
查看>>
设计模式-装饰模式(Decorator)
查看>>
git-子模块submodule
查看>>