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

}
相關文章
相關標籤/搜索