日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UIAlertController的使用及其自定义

發布時間:2025/1/21 编程问答 80 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的使用及其自定义的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。