IOS学习之 -- UIAlertController

AlertView 类型

UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆成功" preferredStyle:UIAlertControllerStyleAlert];
//设置取消按钮
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];   
//设置确定按钮  
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
    UITextField *username = controller.textFields.firstObject;
    NSLog(@"username = %@", username.text);
    UITextField *password = controller.textFields.lastObject;
    NSLog(@"password = %@", password.text);
}];
/*//添加输入框
[controller addTextFieldWithConfigurationHandler:^(UITextField *textField){
    textField.placeholder = @"用户名";
}];
[controller addTextFieldWithConfigurationHandler:^(UITextField *passwrod){
    passwrod.placeholder = @"密码";
    passwrod.secureTextEntry = YES;
}];*/
[controller addAction:cancelAction];
[controller addAction:okAction];
[self presentViewController:controller animated:YES completion:nil];

ActionSheet 类型

UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"请选择" message:@"股市有风险" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *save = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
    NSLog(@"title = %@", action.title);
}];
UIAlertAction *delet = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
    NSLog(@"title = %@", action.title);
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
    NSLog(@"title = %@", action.title);
}];
[controller addAction:save];
[controller addAction:delet];
[controller addAction:cancel];
[self presentViewController:controller animated:YES completion:nil];

你可能感兴趣的:(IOS学习之 -- UIAlertController)