iOS_UIAlertController

 

复制代码
ViewController.m文件


#import "ViewController.h"
@interface ViewController ()
//
@property (nonatomic, strong) UIAlertController *alert;
@property (nonatomic, strong) UIAlertController *actionSheet;
@property (nonatomic, weak) IBOutlet UIButton *showAlertBtn;
@property (nonatomic, weak) IBOutlet UIButton *showActionSheetBtn;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    //创建UIAlertController
    self.alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"消息" preferredStyle:UIAlertControllerStyleAlert];
    //动作
    UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"点击了取消");
    }];
    //动作
    UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"点击了确定");
    }];
    //动作
    UIAlertAction *act3 = [UIAlertAction actionWithTitle:@"销毁" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        NSLog(@"点击了销毁");
    }];
    //将所有动作添加到控制器中
    [self.alert addAction:act1];
    [self.alert addAction:act2];
    [self.alert addAction:act3];
    
    self.actionSheet = [UIAlertController alertControllerWithTitle:@"标题" message:@"消息" preferredStyle:UIAlertControllerStyleActionSheet];
    // 在action sheet中,UIAlertActionStyleCancel不起作用
    UIAlertAction *act4 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"点击了取消");
    }];
    UIAlertAction *act5 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"点击了确定");
    }];
    UIAlertAction *act6 = [UIAlertAction actionWithTitle:@"销毁" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        NSLog(@"点击了销毁");
    }];
    [self.actionSheet addAction:act4];
    [self.actionSheet addAction:act5];
    [self.actionSheet addAction:act6];
}
//显示单击事件
- (IBAction)showAlert:(id)sender {
    [self presentViewController:self.alert animated:YES completion:^{
        
    }];
}
- (IBAction)showActionSheet:(id)sender {

    [self presentViewController:self.actionSheet animated:YES completion:^{
        
    }];
}
@end
复制代码

运行效果图:

iOS8之后用UIAlertController代替了UIAlertView,所以每次有需要弹窗的时候,都需要先判断系统,最近在做的项目中弹窗较多,如果每次都判断,真是太麻烦了,索性对UIAlertController和UIAlertView进行的封装了,封装在一个工具类中,在工具类中就对系统进行判断,然后在你需要弹窗的界面直接调用这个工具类的方法就可以了,减少了代码的耦合.

UIAlertTool.h文件


#import <Foundation/Foundation.h>

@interface UIAlertTool : NSObject
-(void)showAlertView:(UIViewController *)viewController :(NSString *)title :(NSString *)message :(NSString *)cancelButtonTitle :(NSString *)otherButtonTitle :(void (^)())confirm :(void (^)())cancle;
;@end
复制代码
UIAlertTool.m文件

#import "UIAlertTool.h"

#define IAIOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)#import "UIAlertTool.h"typedef void (^confirm)();
typedef void (^cancle)();
@interface UIAlertTool()
{
    confirm confirmParam;
    canclecancleParam;}
@end

@implementation UIAlertTool
-(void)showAlertView:(UIViewController *)viewController :(NSString *)title :(NSString *)message :(NSString *)cancelButtonTitle :(NSString *)otherButtonTitle :(void (^)())confirm :(void (^)())cancle
    {
        confirmParam=confirm;
        cancleParam=cancle;
        if (IAIOS8)
        {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
            // Create the actions.
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action)
                {
                    cancle();
                }];
            UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
            {
                confirm();
            }];
            // Add the actions.
            [alertController addAction:cancelAction];
            [alertController addAction:otherAction];
            [viewController presentViewController:alertController animated:YES completion:nil];
        }
        else
        {
            UIAlertView *TitleAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:otherButtonTitle otherButtonTitles:cancelButtonTitle,nil];
            [TitleAlert show];
        }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0)
    {
        confirmParam();
    }
    else{
        cancleParam();
    }
}
@end
复制代码

 

posted @   Mr·Xu  阅读(293)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示