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);
}
相關文章
相關標籤/搜索