版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/aaaaazq/article/details/80970126
警告弹框和操作列表在开发中常用到,iOS9之后,UIAlertView和UIActionSheet都会报黄色的警告,但是还是依然可以使用的。在这里主要介绍一在这三个控件在Objective-C和Swift下的使用代码。
Objective-C代码:
#import "ViewController.h"
@interface ViewController ()<UIAlertViewDelegate,UIActionSheetDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
NSArray * titleArray = @[@"UIAlertView",@"UIActionSheet",@"UIAlertController alertView",@"UIAlertController actionSheet",@"UIAlertController alertView textField"];
for (int i = 0; i < titleArray.count; i ++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(30, 150 + i*60, self.view.frame.size.width - 60, 45);
button.tag = 300 + i;
[button setTitle:titleArray[i] forState:UIControlStateNormal];
button.backgroundColor = [UIColor orangeColor];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
-(void)buttonClick:(UIButton *)button{
if (button.tag == 300) {
//UIAlertView
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你确定要放弃么?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
}else if (button.tag == 301){
//UIActionSheet
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:@"请选择性别" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"男",@"女", nil];
[actionSheet showInView:self.view];
}else if (button.tag == 302){
//UIAlertController alertView
UIAlertController * alertView = [UIAlertController alertControllerWithTitle:@"提示" message:@"你确定要放弃么" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * canclAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//取消
NSLog(@"点击了取消按钮");
}];
[alertView addAction:canclAction];
UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//确定
NSLog(@"点击了确定按钮");
}];
[alertView addAction:okAction];
[self presentViewController:alertView animated:YES completion:nil];
}else if(button.tag == 303){
//UIAlertController actionSheet
UIAlertController * actionSheet = [UIAlertController alertControllerWithTitle:@"选择性别" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[actionSheet addAction:[UIAlertAction actionWithTitle:@"男"style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
NSLog(@"选择了男");
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:@"女"style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
NSLog(@"选择了女");
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:actionSheet animated:YES completion:nil];
}else{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请输入密码" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.textColor = [UIColor redColor];
}];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了确定按钮");
}]];
[self presentViewController:alert animated:YES completion:nil];
}
}
#pragma mark - UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//实现UIAlertView的点击事件
if (buttonIndex == 0) {
//取消
NSLog(@"点击了取消按钮");
}else if (buttonIndex == 1){
//确定
NSLog(@"点击了确定按钮");
}
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
//实现UIActionSheet的点击事件
if (buttonIndex == 0) {
//取消
NSLog(@"选择了男");
}else if (buttonIndex == 1){
//男
NSLog(@"选择了女");
}else if (buttonIndex == 2){
//女
NSLog(@"点击了取消按钮");
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Swift代码:
import UIKit
class ViewController: UIViewController,UIAlertViewDelegate,UIActionSheetDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setupView()
}
func setupView() {
let titleArray = ["UIAlertView","UIActionSheet","UIAlertController alertView","UIAlertController actionSheet","UIAlertController alertView textField"]
for i in 0...titleArray.count - 1 {
let button = UIButton.init(type: UIButtonType.custom)
button.tag = 300 + i
button.frame = CGRect.init(x: 30, y:(150 + i*60), width:(Int(self.view.frame.size.width - 60)), height: 45)
button.setTitle(titleArray[i], for: UIControlState.normal)
button.backgroundColor = UIColor.orange
button.addTarget(self, action: #selector(buttonClick(_button:)), for:.touchUpInside)
self.view.addSubview(button)
}
}
@objc func buttonClick(_button:UIButton){
if _button.tag == 300 {
//let alertView = UIAlertView.init(title: "提示", message: "你确定要放弃么", delegate: self, cancelButtonTitle: "确定")
let alertView = UIAlertView.init(title: "提示", message: "你确定要放弃么", delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "再想想","确定")
alertView.show()
}else if _button.tag == 301{
let actionSheet = UIActionSheet.init(title: "请选择性别", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle:nil, otherButtonTitles: "男", "女")
actionSheet.show(in: self.view)
}else if _button.tag == 302{
let alertView = UIAlertController.init(title: "提示", message: "你确定要放弃么", preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.cancel) { (action:UIAlertAction) in
//取消
print("点击了取消")
}
alertView.addAction(cancelAction)
let thinkAction = UIAlertAction.init(title: "再想想", style: UIAlertActionStyle.default) { (action:UIAlertAction) in
//再想想
print("点击了再想想")
}
alertView.addAction(thinkAction)
let okAction = UIAlertAction.init(title: "确定", style: UIAlertActionStyle.destructive) { (action:UIAlertAction) in
//确定
print("点击了确定")
}
alertView.addAction(okAction)
self.present(alertView, animated: true, completion: nil)
}else if _button.tag == 303{
let actionSheet = UIAlertController.init(title: "请选性别", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
let cancelAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.cancel) { (action:UIAlertAction) in
//取消
print("点击了取消")
}
actionSheet.addAction(cancelAction)
let boyAction = UIAlertAction.init(title: "男", style: UIAlertActionStyle.default) { (action:UIAlertAction) in
//男
print("点击了男")
}
actionSheet.addAction(boyAction)
let girlAction = UIAlertAction.init(title: "女", style: UIAlertActionStyle.default) { (action:UIAlertAction) in
//女
print("点击了女")
}
actionSheet.addAction(girlAction)
self.present(actionSheet, animated: true, completion: nil)
}else{
let alertTextField = UIAlertController.init(title: "请输入密码", message: nil, preferredStyle: UIAlertControllerStyle.alert)
alertTextField.addTextField { (textField:UITextField) in
textField.isSecureTextEntry = true
}
let okAction = UIAlertAction.init(title: "确定", style: UIAlertActionStyle.destructive) { (action:UIAlertAction) in
//确定
print("点击了确定")
}
alertTextField.addAction(okAction)
self.present(alertTextField, animated: true, completion: nil)
}
}
//MARK:-UIAlertViewDelegate
func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
if buttonIndex == 0 {
//取消
print("点击了取消")
}else if buttonIndex == 1 {
//再想想
print("点击了再想想")
}else if buttonIndex == 2 {
//确定
print("点击了确定")
}
}
//MARK:-UIActionSheetDelegate
func actionSheet(_ actionSheet: UIActionSheet, clickedButtonAt buttonIndex: Int) {
if buttonIndex == 0 {
//取消
print("点击了取消")
}else if buttonIndex == 1 {
//男
print("点击了男")
}else if buttonIndex == 2{
//女
print("点击了女")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
以上给出的代码都是完整代码,复制粘贴可用~