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
  • 相关阅读:
    Java 单链表的倒置
    Android查询:模拟键盘鼠标事件(adb shell 实现)
    安卓 发送短信两种方式
    java tcp socket实例
    Java中读取某个目录下的所有文件和文件夹
    Android剖析和运行机制
    linux下搭建android NDK开发环境
    把log存起来
    判断Android系统net和wap接入点的开发实例
    android 4.0.4系统下实现apk的静默安装和启动
  • 原文地址:https://www.cnblogs.com/xjf125/p/4853499.html
Copyright © 2011-2022 走看看