UIAlertController的使用及其自定义
2019獨角獸企業重金招聘Python工程師標準>>>
UIAlertController的正常用法
// 上傳
- (void)btnClick{
? ? /* preferredStyle有且只有這兩種枚舉類型
?? ? * ? ? UIAlertControllerStyleActionSheet? 在屏幕底部彈出
?? ? * ? ? UIAlertControllerStyleAlert? 在屏幕中間彈出
?? ? */
//? ? UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"標題" message:@"副標題" preferredStyle:UIAlertControllerStyleAlert];? ??
? ? UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"標題" message:@"副標題" preferredStyle:UIAlertControllerStyleActionSheet];? ??
? ? UIAlertAction *Action1 = [UIAlertAction actionWithTitle:@"普通按鈕" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
? ? ? ? //普通按鈕
? ? ? ? NSLog(@"普通按鈕是藍色的");
? ? }];? ??
? ? UIAlertAction *Action2 = [UIAlertAction actionWithTitle:@"警告按鈕" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *_Nonnull action) {
? ? ? ? //普通按鈕
? ? ? ? NSLog(@"帶有警告意味的按鈕是紅色的");
? ? }];? ??
? ? UIAlertAction *Action3 = [UIAlertAction actionWithTitle:@"取消按鈕" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
? ? ? ? //普通按鈕
? ? ? ? NSLog(@"取消按鈕也是藍色的,并且始終在最下面");
? ? }];
?? ?
//? ? //如果是UIAlertControllerStyleActionSheet不能使用添加輸入框的方法
//? ? [alertControl addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//? ? ? ? //添加輸入框(已經自動add,不需要手動)
// ? ? ? ?
//? ? ? ? textField.text = @"可以在這里寫textfield的一些屬性";
// ? ? ? ?
//? ? ? ? //監聽
//? ? ? ? [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(listening:) name:UITextFieldTextDidChangeNotification object:textField];
//? ? ? ? }];
?? ?
? ? [alertControl addAction:Action1];
? ? [alertControl addAction:Action2];
? ? [alertControl addAction:Action3];? ??
? ? [self presentViewController:alertControl animated:YES completion:nil];? ??
}
監聽輸入框的輸入
//- (void)listening:(NSNotification *)noti{
//? ? NSLog(@"%@", noti.object);
//}
UIAlertControllerStyleAlert??在屏幕中間彈出UIAlertControllerStyleActionSheet??在屏幕底部彈出
? ?
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
UIAlertController的自定義(利用富文本與kvc)
? ? UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"標題" message:@"副標題" preferredStyle:UIAlertControllerStyleAlert];
? ? // 自定義標題
? ? NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"這是標題"];
? ? [title addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0,4)];
? ? [title addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,4)];
? ? [alertControl setValue:title forKey:@"attributedTitle"];? ?
? ? // 自定義副標題
? ? NSMutableAttributedString *subTitle = [[NSMutableAttributedString alloc] initWithString:@"這是副標題"];
? ? [subTitle addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0,5)];
? ? [subTitle addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0,5)];
? ? [alertControl setValue:subTitle forKey:@"attributedMessage"];
? ? // 自定義取消按鈕
? ? UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];? ??
? ? // 設置按鈕背景圖片
? ? UIImage *image = [[UIImage imageNamed:@"icon_182"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
? ? [cancelAction setValue:image forKey:@"image"];? ??
? ? // 設置按鈕的title顏色
? ? [cancelAction setValue:[UIColor orangeColor] forKey:@"titleTextColor"];? ??
? ? // 設置按鈕的title的對齊方式
? ? [cancelAction setValue:[NSNumber numberWithInteger:NSTextAlignmentLeft] forKey:@"titleTextAlignment"];
? ? UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確認" style:UIAlertActionStyleDefault handler:nil];
? ? [alertControl addAction:okAction];
? ? [alertControl addAction:cancelAction];? ??
? ? [self presentViewController:alertControl animated:YES completion:nil];? ??
效果圖:
轉載于:https://my.oschina.net/yejiexiaobai/blog/800781
總結
以上是生活随笔為你收集整理的UIAlertController的使用及其自定义的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 偶遇mysql(Percona Serv
- 下一篇: YOLO 训练