Skip to content

Instantly share code, notes, and snippets.

@jinjereu
Last active June 11, 2019 10:52
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 jinjereu/11b8a5a463a25ca30b949078e85304d2 to your computer and use it in GitHub Desktop.
Save jinjereu/11b8a5a463a25ca30b949078e85304d2 to your computer and use it in GitHub Desktop.
Show Alert with textfield in Objective-C
@implementation AlertWithTextField
- (void)showAlertInViewController:(UIViewController *)vc{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"New Name"
__weak typeof(ViewController) *weakSelf = self;
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"Enter name";
}];
UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (!alert.textFields) {
return;
}
NSString *stringToSave = [alert.textFields firstObject].text;
if (![stringToSave isEqualToString:@""]) {
NSLog(@"Show me the input: %@", stringToSave);
}
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:nil];
[alert addAction:saveAction];
[alert addAction:cancelAction];
[vc presentViewController:alert animated:YES completion:nil];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment