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

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


设置中间的弹出框ide

UIAlertControllerspa


#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{
    
    //iOS9.0以后用新方法
    //新建一个弹出框alert
    //后面的能够选从底部弹出
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请检查网络" preferredStyle:UIAlertControllerStyleAlert];
    
    //新建一个输入用户名的框
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.keyboardType = UIAlertActionStyleDefault;
    }];
    
    
    //新建一个输入密码的框
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
        //设置密码格式(输入后是小点)
        textField.secureTextEntry = YES;
        //设置输入框弹出键盘样式(这里设置为数字键盘)
        textField.keyboardType = UIKeyboardTypeNumberPad;
        //设置键盘右下角return键的类型
        textField.returnKeyType = UIReturnKeyYahoo;
    }];
    
    
    //设置取消按钮和点击后的后续操做响应事件
    //在handler:处直接按键盘上的回车键,就会展开
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"用户点击了取消");
    }]];
    
    
    //设置确认按钮和点击后的后续操做响应事件
    //!!!其实这里都同样,只是“确认”两字不同,后面还能够加好多个(视具体状况而定,设置本身想要的功能按钮就行了)
    [alert addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"用户点击了确认");
        
        //好比:若是点击了确认,就打印输入的用户名和密码
        NSLog(@"%@, %@", [[alert textFields]objectAtIndex:0].text
              ,          [[alert textFields]objectAtIndex:1].text);
        
        
    }]];
    
    
    //将设置赋给弹出框,而且能够弹出,很重要!!!
    [self presentViewController:alert animated:YES completion:nil];
    

}
相关文章
相关标签/搜索