zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    两个进程之间的通讯——pipe 管道
    if if 和 if elif 的区别
    python处理大文件——文件流处理
    DNA甲基化及其测量方法(转)
    windows 服务器安装python及其基本库
    在linux系统个人目录下安装新版python
    python 列表中 [[], [], []] 和 3*[[]]差异
    Python脚本打包为exe文件
    GC偏好的校正与偏好程度的评估
    荧光分子的dynamic quenching 和 通常说的quenching的区别?
  • 原文地址:https://www.cnblogs.com/xjf125/p/4853499.html
Copyright © 2011-2022 走看看