// 方式1 无代理,只有一个确定按钮(左右排列,取消按钮在左,确定按钮在右)
UIAlertView *alertview001 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"按钮点击后我才出现的。" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"知道了", nil];
[alertview001 show];
// 方式2 无代理,有多个确定按钮(多个确定按列表排列,且取消按钮在最下面)
UIAlertView *alertview002 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"按钮点击后我才出现的。" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"知道0", @"知道1", @"知道2", @"知道3", @"知道4", nil];
[alertview002 show];
// 方法3 有代理,只有一个确定按钮
/*
1 设置代理为 self
2 设置代理协议
3 实现代理方法
*/
UIAlertView *alertview003 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"按钮点击后我才出现的。" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"知道0", nil];
[alertview003 show];
// 设置协议
@interface ViewController () <UIAlertViewDelegate>
@end
// UIAlertViewDelegate实现代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"buttonIndex=%ld", buttonIndex);
// 方法1
// if (0 == buttonIndex)
// {
// NSLog(@"点击了取消按钮");
// }
// else if (1 == buttonIndex)
// {
// NSLog(@"点击了确定按钮");
// }
// 方法2
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
BOOL isEqual = [title isEqualToString:@"知道0"];
if (isEqual)
{
NSLog(@"点击了确定按钮");
}
else
{
NSLog(@"点击了取消按钮");
}
}
// 方法4 有代理,只有一个确定按钮,输入模式
UIAlertView *alertview004 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"请输入信息" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"知道0", nil];
// 默认模式,无输入框
// alertview004.alertViewStyle = UIAlertViewStyleDefault;
// 输入不可见文本的密码模式,有一个密码输入框
// alertview004.alertViewStyle = UIAlertViewStyleSecureTextInput;
// 输入可见文本模式,有一个文本输入框
// alertview004.alertViewStyle = UIAlertViewStylePlainTextInput;
// 输入可见文本与不可见文本模式,即登录输入模式,有两个输入框
alertview004.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
// 第一个输入框
UITextField *nameField = [alertview004 textFieldAtIndex:0];
nameField.placeholder = @"请输入登录帐号";
// 第二个输入框
UITextField *passwordField = [alertview004 textFieldAtIndex:1];
passwordField.placeholder = @"请输入登录密码";
// 显示
[alertview004 show];
// 设置协议
@interface ViewController () <UIAlertViewDelegate>
@end
// UIAlertViewDelegate实现代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"buttonIndex=%ld", buttonIndex);
// 方法1
// if (0 == buttonIndex)
// {
// NSLog(@"点击了取消按钮");
// }
// else if (1 == buttonIndex)
// {
// NSLog(@"点击了确定按钮");
// }
// 方法2
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
BOOL isEqual = [title isEqualToString:@"知道0"];
if (isEqual)
{
NSLog(@"点击了确定按钮");
// 输入可见文本与不可见文本模式,即登录输入模式,有两个输入框
// 第一个输入框
UITextField *nameField = [alertView textFieldAtIndex:0];
NSString *name = nameField.text;
NSLog(@"name=%@",name);
// 第二个输入框
UITextField *passwordField = [alertView textFieldAtIndex:1];
NSString *password = passwordField.text;
NSLog(@"password=%@",password);
}
else
{
NSLog(@"点击了取消按钮");
}
}
// 方法5
/*
iOS8以后出现了UIAlertController视图控制器,通过设置UIAlertController的style属性来控制是alertview还是actionsheet
*/
UIAlertController *alertContoller = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"按钮点击后我才出现的。" preferredStyle:UIAlertControllerStyleAlert];
// 响应方法-取消
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了取消按钮");
}];
// 响应方法-确定
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了确定按钮");
}];
// 添加响应方式
[alertContoller addAction:cancelAction];
[alertContoller addAction:confirmAction];
// 显示
[self presentViewController:alertContoller animated:YES completion:nil];