簡單說一下UIAlertController的基本使用方法,廢話不多說,直接上代碼
1.基本的使用方法
//創(chuàng)建控制器
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"\n此用戶不存在請(qǐng)去添加" preferredStyle:UIAlertControllerStyleAlert];
//創(chuàng)建添加按鈕(可以寫事件,也可以不寫),可以創(chuàng)建多個(gè)按鈕
UIAlertAction *OKbtn = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
//添加按鈕到alertVC上,并模態(tài)出來
[alertVC addAction:OKbtn];
[self presentViewController:alertVC animated:YES completion:nil];

7C7949D6-3F86-4AEC-8D67-5C8A202A3602.png
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"\n是否進(jìn)行關(guān)聯(lián)" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *addBtn = [UIAlertAction actionWithTitle:@"關(guān)聯(lián)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertVC addAction:cancel];
[alertVC addAction:addBtn];
[self presentViewController:alertVC animated:YES completion:nil];

C32A8434-DF23-4574-ADE7-586283FB5C52.png
添加文本框,因?yàn)槲谋究虻拇笮≌{(diào)起來不太方便,所以這里有個(gè)小技巧,文本框的大小可以跟著字體的大小來變動(dòng)
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"請(qǐng)輸入文件名稱" preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//文本框占位文字
textField.placeholder = @"請(qǐng)輸入文件名稱";
//改變字體大小
[textField setFont:[UIFont systemFontOfSize:17]];
//定義一個(gè)文本框來接收,傳值用
nameTextField = textField;
}];
UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *addBtn = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:cancelBtn];
[alert addAction:addBtn];
[self presentViewController:alert animated:YES completion:nil];

1A83D03B-DBCC-4C77-BAC7-1A0D8A5698FF.png
文本框比系統(tǒng)原來的要大一點(diǎn),調(diào)大字體,文本框可以自適應(yīng),根據(jù)項(xiàng)目需求來吧
改變字體顏色
//創(chuàng)建控制器,不需要設(shè)置文字
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert];
//修改title
NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:@"提 示"];
//設(shè)置顏色
[alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, alertControllerStr.length)];
//設(shè)置大小
[alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, alertControllerStr.length)];
//替換title
[alertVC setValue:alertControllerStr forKey:@"attributedTitle"];
//修改message
NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:@"\n此用戶不存在請(qǐng)去添加"];
//設(shè)置文字大小,并沒有設(shè)置文字顏色
[alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, alertControllerMessageStr.length)];
//進(jìn)行替換
[alertVC setValue:alertControllerMessageStr forKey:@"attributedMessage"];
//確定按鈕
UIAlertAction *OKbtn = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
//添加
[alertVC addAction:OKbtn];
[self presentViewController:alertVC animated:YES completion:nil];
至于按鈕的顏色,系統(tǒng)的style有紅色可以選擇,UIAlertActionStyleDestructive。

8AE31341-130C-45B8-8A2F-A88DDBFADF90.png
到這里,UIAlertController的使用基本可以滿足日常的開發(fā)需求了,如果有特殊需求,可以繼續(xù)深入研究!!祝各位工作順利?。?!