Skip to content

Instantly share code, notes, and snippets.

@D-32
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save D-32/2aaa0b6959e253a3d93a to your computer and use it in GitHub Desktop.
Save D-32/2aaa0b6959e253a3d93a to your computer and use it in GitHub Desktop.
UIAlertController with text field check
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title"
message:nil
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAlertAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
_saveAlertAction = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
}];
_saveAlertAction.enabled = NO;
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Enter a title...";
[textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
}];
[alert addAction:cancelAlertAction];
[alert addAction:_saveAlertAction];
[self presentViewController:alert animated:YES completion:nil];
// ----------------------
- (void)textChanged:(id)sender
{
UITextField *field = sender;
_saveAlertAction.enabled = field.text.length > 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment