答:廢話不多說(shuō)直接上代碼。如下所示:
/*
類方法快速創(chuàng)建一個(gè)提示控制器 值得注意的是這個(gè)控制器有個(gè)preferreStyle屬性你可以根據(jù)這個(gè)屬性來(lái)確定是使用UIAlertView 還是 UIActionSheet
UIAlertControllerStyleActionSheet
UIAlertControllerStyleAlert
*/
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"顯示的標(biāo)題" message:@"標(biāo)題的提示信息" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點(diǎn)擊取消");
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點(diǎn)擊確認(rèn)");
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"警告" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點(diǎn)擊警告");
}]];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
NSLog(@"添加一個(gè)textField就會(huì)調(diào)用 這個(gè)block");
}];
// 由于它是一個(gè)控制器 直接modal出來(lái)就好了
[self presentViewController:alertController animated:YES completion:nil];
原文:http://www.cnblogs.com/zhangguoliang1992/p/4918684.html