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

旧方法:iOS9.0之后用新方法网络


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

UIAlertViewspa

#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];
    }

}



从底部弹出一个警告框代理

UIActionSheetcode

#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);
}
相关文章
相关标签/搜索