看到網(wǎng)上還一直是uialertview的天下, 決定寫個(gè)兼容, 畢竟這個(gè)隨時(shí)被淘汰
寫的并不靈活, 但包含了文本輸入, 其中的按鈕事件要分開寫
頭文件
#import <UIKit/UIKit.h>
@interface UIAlertPlus : NSObject
+(void)showAlertWithTitle:(NSString*)title msg:(NSString*)msg delegate:(id)delegate cancelBt:(NSString*)cancel otherBt:(NSString*)other inputTip:(NSString*)inputTip cancelBlock:(void (^ __nullable)(UIAlertController* alert, UIAlertAction *action))cancelBlock okBlock:(void (^ __nullable)(UIAlertController* alert, UIAlertAction *action))okBlock;
@end
實(shí)現(xiàn)文件
//
// UIAlertPlus.m
// TestDemoConfig
//
// Created by Zszen John on 2017/7/9.
// Copyright ? 2017年 Zstudio. All rights reserved.
//
#import "UIAlertPlus.h"
@implementation UIAlertPlus
+(void)showAlertWithTitle:(NSString*)title msg:(NSString*)msg delegate:(id)delegate cancelBt:(NSString*)cancel otherBt:(NSString*)other inputTip:(NSString*)inputTip cancelBlock:(void (^ __nullable)(UIAlertController* alert, UIAlertAction *action))cancelBlock okBlock:(void (^ __nullable)(UIAlertController* alert, UIAlertAction *action))okBlock{
if([[[UIDevice currentDevice] systemVersion]intValue]<8){
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:delegate cancelButtonTitle:cancel otherButtonTitles:other, nil];
if (inputTip==nil) {
alertView.alertViewStyle = UIAlertViewStyleDefault;
}else{
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *txtInput = [alertView textFieldAtIndex:0];//設(shè)置輸入框
txtInput.placeholder=inputTip;
txtInput.delegate=delegate;
}
[alertView show];
}else{
UIAlertController* alertController = [UIAlertController alertControllerWithTitle:title
message:msg
preferredStyle:UIAlertControllerStyleAlert];//
if(other!=nil && okBlock!=nil){
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:other style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if(okBlock!=nil) okBlock(alertController,action);
}];
[alertController addAction:defaultAction];
}
if(cancel!=nil && cancelBlock!=nil){
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:cancel style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
if(cancelBlock!=nil) cancelBlock(alertController,action);
}];
[alertController addAction:cancelAction];
}
if(inputTip!=nil){
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = inputTip;
}];
}
[delegate presentViewController:alertController animated:YES completion:nil];
}
}
@end
運(yùn)用方法:
{
//in function
[UIAlertPlus showAlertWithTitle:@"保存到剪貼板" msg:nil delegate:self cancelBt:@"取消" otherBt:@"確定" inputTip:@"輸入濾鏡名稱" cancelBlock:nil okBlock:^(UIAlertController *alert, UIAlertAction *action) {
UITextField* txtInput = alert.textFields.firstObject;
[self snipToClipboard:txtInput];
}];
}
//ios7 支持
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//只寫了點(diǎn)擊確定按鈕的事件可以根據(jù)需要來設(shè)置多個(gè)按鈕的點(diǎn)擊事件
if (buttonIndex == alertView.firstOtherButtonIndex) {
UITextField *txtInput = [alertView textFieldAtIndex:0];
[self snipToClipboard:txtInput];
}
}