Last active
June 11, 2019 10:52
-
-
Save jinjereu/11b8a5a463a25ca30b949078e85304d2 to your computer and use it in GitHub Desktop.
Show Alert with textfield in Objective-C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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