您的位置:首页 > 产品设计 > UI/UE

iOS8的UIAlertController

2016-06-29 00:00 429 查看
好吧 我很懒, 我看这太简单了, 直接贴api了. 作为一个iOSer, 这东西两分钟都看完了, 对不?

1. UIAlertController类

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

2. UIAlertAction类以及结构体

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

3. 注意点

测试发现, 当有一个
UIAlertAction
是取消类型时候, 不管这个action是第几个被添加进去的, 都会被放在最底部,如图:



- (void)tapGestureClick:(UITapGestureRecognizer *)tapGesture {

NSLog(@"点了图片");

UIAlertController *alertSheet = [UIAlertController alertControllerWithTitle:@"提醒" message:@"请选择图片来源" preferredStyle:0];

UIAlertAction *takePicture = [UIAlertAction actionWithTitle:@"拍一张" style:0 handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"拍一张");
}];

UIAlertAction *photoAlbum = [UIAlertAction actionWithTitle:@"从相册中选择" style:0 handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"从相册中选择");
}];

UIAlertAction *cancleBtn = [UIAlertAction actionWithTitle:@"取消" style:1 handler:nil];

[alertSheet addAction:cancleBtn];
[alertSheet addAction:takePicture];
[alertSheet addAction:photoAlbum];

[self presentViewController:alertSheet animated:YES completion:nil];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: