Enable UIAlertAction of UIAlertController only after input

前端 未结 6 1705
感情败类
感情败类 2020-12-16 01:39

I am using a UIAlertController to present a dialog with a UITextField and one UIAlertAction button labeled \"Ok\". How do I disable th

6条回答
  •  渐次进展
    2020-12-16 01:54

    A better approach would be to alert the user about what is wrong with his input after validating his input, so that the user knows what the app is expecting from him.

    - (void)askReasonWithPreviousReason:(NSString *)text
    {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Enter reason" preferredStyle:UIAlertControllerStyleAlert];
    
        [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField)
         {
             textField.text = text;
         }];
    
        [alertController addAction:[UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
                                    {
                                        if ([self isReasonValid:alertController.textFields.firstObject.text])
                                        {
                                            UIAlertController *alertController2 = [UIAlertController alertControllerWithTitle:AlertTitle message:@"Are you sure you would like to save?" preferredStyle:UIAlertControllerStyleAlert];
    
                                            [alertController2 addAction:[UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
                                                                         {
                                                                             [self saveReason:alertController.textFields.firstObject.text];
                                                                         }]];
                                            [alertController2 addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:Nil]];
    
                                            [self presentViewController:alertController2 animated:YES completion:nil];
                                        }
                                    }]];
    
        [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:Nil]];
    
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
    - (BOOL)isReasonValid:(NSString *)reason
    {
        NSString *errorMessage = [[NSString alloc] init];
    
        if (reason.length < 5)
        {
            errorMessage = @"Reason must be more than 5 characters";
        }
        else if (reason.length > 100)
        {
            errorMessage = @"Reason must be less than 100 characters";
        }
    
        if (errorMessage.length != 0)
        {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert" message:errorMessage preferredStyle:UIAlertControllerStyleAlert];
    
            [alertController addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
                                        {
                                            [self askReasonWithPreviousReason:reason];
                                        }]];
    
            [self presentViewController:alertController animated:YES completion:nil];
    
            return NO;
        }
    
        return YES;
    }
    

提交回复
热议问题