警告彈框和操作清單在開發中常用到,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 = ; i < titleArray.count; i ++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(, + i*, self.view.frame.size.width - , );
button.tag = + 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 == ) {
//UIAlertView
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你确定要放棄麼?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
}else if (button.tag == ){
//UIActionSheet
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:@"請選擇性别" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"男",@"女", nil];
[actionSheet showInView:self.view];
}else if (button.tag == ){
//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 == ){
//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 == ) {
//取消
NSLog(@"點選了取消按鈕");
}else if (buttonIndex == ){
//确定
NSLog(@"點選了确定按鈕");
}
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
//實作UIActionSheet的點選事件
if (buttonIndex == ) {
//取消
NSLog(@"選擇了男");
}else if (buttonIndex == ){
//男
NSLog(@"選擇了女");
}else if (buttonIndex == ){
//女
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 ..titleArray.count - {
let button = UIButton.init(type: UIButtonType.custom)
button.tag = + i
button.frame = CGRect.init(x: , y:( + i*), width:(Int(self.view.frame.size.width - )), height: )
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 == {
//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 == {
let actionSheet = UIActionSheet.init(title: "請選擇性别", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle:nil, otherButtonTitles: "男", "女")
actionSheet.show(in: self.view)
}else if _button.tag == {
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 == {
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 == {
//取消
print("點選了取消")
}else if buttonIndex == {
//再想想
print("點選了再想想")
}else if buttonIndex == {
//确定
print("點選了确定")
}
}
//MARK:-UIActionSheetDelegate
func actionSheet(_ actionSheet: UIActionSheet, clickedButtonAt buttonIndex: Int) {
if buttonIndex == {
//取消
print("點選了取消")
}else if buttonIndex == {
//男
print("點選了男")
}else if buttonIndex == {
//女
print("點選了女")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
以上給出的代碼都是完整代碼,複制粘貼可用~