Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.