使用UIAlertController實現帶輸入框的alertView和按鈕動態可用

UIAlertController設置按鈕動態可用
// 定義全局變量
UITextField *textField1;
UITextField *textField2;

UIAlertAction *cancelAction;
UIAlertAction *okAction;
- (void)showAlertController {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
    
    cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"點擊了Cancel");
    }];
    
    okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"點擊了OK");
    }];
    
    [alertController addAction:okAction];
    [alertController addAction:cancelAction];
    
    // 添加文本框(只能添加到UIAlertControllerStyleAlert的樣式,如果是preferredStyle:UIAlertControllerStyleActionSheet則會崩潰)
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        
        textField.placeholder = @"Login";
        
        // 監聽文字改變的方法,也可以通過通知
        [textField addTarget:self action:@selector(textFieldDidChange:)forControlEvents:UIControlEventEditingChanged];
        
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];

        textField1 = textField;
    }];
    
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        
        textField.secureTextEntry = YES;  // 密文形式顯示
        textField.placeholder = @"Password";
        
        // 監聽文字改變的方法
        [textField addTarget:self action:@selector(textFieldDidChange:)forControlEvents:UIControlEventEditingChanged];
        
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];
        
        textField2 = textField;
    }];
    
    
    if (textField1.text.length > 0 && textField2.text.length > 0) {
        okAction.enabled = YES;
    } else {
        okAction.enabled = NO;
    }
    
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}

// 監聽文字改變的方法
- (void)textFieldDidChange:(UITextField *)textField {
    
    if (textField1.text.length > 0 && textField2.text.length > 0) {
        okAction.enabled = YES;
    } else {
        okAction.enabled = NO;
    }
}

// 通過通知來監聽文字改變
- (void)textFieldDidChangeNotification:(NSNotification *)notification {
    
    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
    
    if (alertController) {
        
//        UITextField *login = alertController.textFields.firstObject;
//        UITextField *passw = alertController.textFields.lastObject;
        
//        UIAlertAction *cancelAction = alertController.actions.firstObject;
//        UIAlertAction *okAction = alertController.actions.lastObject;
        
        if (textField1.text.length > 0 && textField2.text.length > 0) {
            okAction.enabled = YES;
        } else {
            okAction.enabled = NO;
        }
    }
}

使用UIAlertView實現帶輸入框的alertView及設置鍵盤樣式,按鈕動態可用

使用UIAlertController實現帶輸入框的alertView和按鈕動態可用

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容