在IOS8之后,UIAlertController替代了UIActionSheet和UIAlertView.由一个控制器来管理 操作方便, 而且每个功能键都很清晰。不再使用delegate的方式来触发回调。
代码地址:https://github.com/gujinyue1010/iOS__UIAlertController (其中也包含了UIAlertView和UIActionSheet,有兴趣的童鞋可以fork一下看看)
1.直接上代码:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
UIButton *button=[UIButton buttonWithType:UIButtonTypeContactAdd];
button.frame=CGRectMake(100, 100, 40, 40);
// [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(click1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)click1
{
UIAlertController *alertController=[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *confirm=[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"拍照。。。");
}];
UIAlertAction *destruct=[UIAlertAction actionWithTitle:@"从相册中取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"从相册中取");
}];
UIAlertAction *cancel=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:confirm];
[alertController addAction:destruct];
[alertController addAction:cancel];
[self presentViewController:alertController animated:YES completion:^{
}];
}
-(void)click2
{
UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"" message:@"注销当前帐号" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *confirm=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"确定按钮");
}];
//UIAlertAction *destruct=[UIAlertAction actionWithTitle:@"注销" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
// NSLog(@"执行注销帐号操作");
//}];
UIAlertAction *cancel=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:confirm];
//[alertController addAction:destruct];
[alertController addAction:cancel];
[self presentViewController:alertController animated:YES completion:^{
}];
}
-(void)click5
{
UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"提示" message:@"请输入区域名称" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *confirm=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField *area=alertController.textFields.firstObject;
UITextField *furniture=alertController.textFields.lastObject;
NSLog(@"===%@ %@",area.text,furniture.text);
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder=@"请输入智能区域名称";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder=@"请输入电器名称";
}];
[alertController addAction:cancel];
[alertController addAction:confirm];
[self presentViewController:alertController animated:YES completion:nil];
}
@end
2.效果图:
3.看看苹果官方对于UIAlertController的描述:
#import <UIKit/UIViewController.h>
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
UIAlertActionStyleDefault = 0,
UIAlertActionStyleCancel,
UIAlertActionStyleDestructive
} NS_ENUM_AVAILABLE_IOS(8_0);
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,
UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);
NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertAction : NSObject <NSCopying>
+ (instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler;
@property (nullable, nonatomic, readonly) NSString *title;
@property (nonatomic, readonly) UIAlertActionStyle style;
@property (nonatomic, getter=isEnabled) BOOL enabled;
@end
NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController
+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;
- (void)addAction:(UIAlertAction *)action;
@property (nonatomic, readonly) NSArray<UIAlertAction *> *actions;
@property (nonatomic, strong, nullable) UIAlertAction *preferredAction NS_AVAILABLE_IOS(9_0);
- (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;
@property (nullable, nonatomic, readonly) NSArray<UITextField *> *textFields;
@property (nullable, nonatomic, copy) NSString *title;
@property (nullable, nonatomic, copy) NSString *message;
@property (nonatomic, readonly) UIAlertControllerStyle preferredStyle;
@end
NS_ASSUME_NONNULL_END
UIAlertActionStyle是描述弹框动作样式的(一般用default)。UIAlertControllerstyle是用来描述弹框样式的(UIAlertControllerStyleActionSheet是从底部弹出的,UIAlertControllerStyleAlert是弹出在界面中间的
)。