iOS中的UIAlertView之旧方法(弹出警告框)

iOS警告框使用详解
本文详细介绍了在iOS应用中如何使用UIAlertView和UIActionSheet创建不同样式的警告框和操作菜单,包括从中间或底部弹出的提示框设置及响应处理。

旧方法:iOS9.0以后用新方法


从中间弹出一个警告框,并且设置它的属性

UIAlertView

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置一个Button,作用是点击它,弹出警告框
    UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
    myButton.backgroundColor = [UIColor redColor];
    [myButton setTitle:@"点击" forState:UIControlStateNormal];
    [myButton addTarget:self action:@selector(haha:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myButton];
    
}

//Button的方法
-(void)haha:(id)a{
    
    //弹出一个警告框,如(网络异常,密码不正确等)
    //“网络异常”是中间的大字,“请求设置网络”是下面的小字,“cancel”是下面左边的按钮,“ok”是下面右边的按钮
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"网络异常" message:@"请求设置网络" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
    
    
//    UIAlertViewStyleSecureTextInput
//    UIAlertViewStylePlainTextInput
//    UIAlertViewStyleLoginAndPasswordInput
    
    //弹框上面的文本框样式
    //这里选的是用户名和密码,上面三种都是样式,按住command键点击setAlertViewStyle中都有
    [alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
    
    //设置输入框弹出键盘样式
    //给用户名输入框(位置0)设置了一个 数字键盘 的样式
    [[alert textFieldAtIndex:0]setKeyboardType:UIKeyboardTypeNumberPad];
    
    //给密码输入框(位置1)设置了一个 Web键盘 的样式
    [[alert textFieldAtIndex:1]setKeyboardType:UIKeyboardTypeWebSearch];
    
    //这一句很关键!!!
    [alert show];
    
}

//警告弹框的方法(代理方法,默认就有)判断点击了cancle还是OK,下面就可以分支写代码了
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        NSLog(@"点击了cancle");
    }else{
        NSLog(@"点击了ok");
    }
    
    //判断警告框上输入的用户名和密码是不是正确

    //新建一个UITextField来存储输入的用户名和密码的值
    UITextField *nameTextField = (UITextField *)[alertView textFieldAtIndex:0];
    UITextField *passwordTextField = (UITextField *)[alertView textFieldAtIndex:1];
    
    //判断如果用户名正确与密码正确
    if ([nameTextField.text isEqualToString:@"xiaoming"] && [passwordTextField.text isEqualToString:@"123456"]) {
        //则跳到下一个界面(xixi:是一个方法)
        [self xixi:nil];
    }else{
        //如果不正确,则弹出一个警告框,内容是“账号或密码错误”
        UIAlertView *alertTwo = [[UIAlertView alloc]initWithTitle:@"账号或密码错误" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"取消", nil];
        [alertTwo show];
    }

}



从底部弹出一个警告框

UIActionSheet

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置一个Button,作用是点击它,弹出警告框
    UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
    myButton.backgroundColor = [UIColor redColor];
    [myButton setTitle:@"点击" forState:UIControlStateNormal];
    [myButton addTarget:self action:@selector(haha:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myButton];
    
}

//Button的方法
-(void)haha:(id)a{
    
    //从底部弹出一个警告框
    //“删除文件”是小字内容,“取消”是最下面的选项,“删除后无法启动”和“删除”是中间的选项,“删除后无法启动”是红字警告
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"删除文件" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除后无法启动" otherButtonTitles:@"删除", nil];
    [sheet showInView:self.view];
}


//先按住command点击他的代理<UIActionSheetDelegate>,找出下面的方法,然后点击“删除后无法启动”“删除”“取消”等按钮,找出他们对应的编号(下标),就可以根据下标写对应的方法了
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%ld", (long)buttonIndex);
}





转载于:https://my.oschina.net/LBBB/blog/657534

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值