原文檔:
iOS 自定義UIAlertController的字體囤萤、顏色昼窗、大小
UIAlertController 代替 UIAlertView 和 UIActionSheet
UIAlertController:新東西就是方便好用,代碼量減少涛舍,都不用代理了澄惊,用起來還更簡單了
簡單過程:新建 UIAlertController 對其添加 UIAlertAction ,然后 對ViewController 進(jìn)行 present/dismiss 就可以了做盅。
新版 普通 Alert
// 初始化 添加 提示內(nèi)容
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"messgae" preferredStyle:UIAlertControllerStyleAlert];
/*
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,// 不能加輸入框缤削,其他一樣
UIAlertControllerStyleAlert // 可以添加輸入框
} 彈出類型;
*/
// 添加 AlertAction 事件回調(diào)(三種類型:默認(rèn),取消吹榴,警告)
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"ok");
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"cancel");
}];
UIAlertAction *errorAction = [UIAlertAction actionWithTitle:@"error" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"error");
}];
// cancel類自動變成最后一個,警告類推薦放上面
[alertController addAction:errorAction];
[alertController addAction:okAction];
[alertController addAction:cancelAction];
// 出現(xiàn)
[self presentViewController:alertController animated:YES completion:^{
NSLog(@"presented");
}];
// 移除
[alertController dismissViewControllerAnimated:YES completion:^{
NSLog(@"dismiss");
}];
新版 帶輸入的 Alert
// AlertController 直接添加 textField 即可滚婉!
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"name";
}];
// 添加 action图筹,再其回調(diào)中可以處理輸入內(nèi)容
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// 獲取上面的輸入框
UITextField *tempField = [alertController.textFields firstObject];
NSLog(@"%@",tempField.text);
NSLog(@"ok");
}];
新版 ActionSheet
// 提示內(nèi)容 初始化 AlertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"messgae" preferredStyle:UIAlertControllerStyleActionSheet];
//其余的完全一致,只是添加 TextField 會報(bào)錯而已。