The following code will show the Alert with textField :-
UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"Alert Title" message:nil preferredStyle:UIAlertControllerStyleAlert];
// to add textField
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
// do any textField related code here
}];
We need to dismiss the alert also.. So for that we will add a action to the UIAlert to dismiss it.. Here, its shows two buttons OK and CANCEL added to the UIAlert.. The code is :-
UIAlertAction *updateAction=[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField *txtField = [alert.textFields firstObject];
NSLog(@"Your text %@",txtField.text); // to access the text field value
}];
UIAlertAction *dismissAction=[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
}];
[alert addAction:updateAction];
[alert addAction:dismissAction];
Its working !!
0 Comment(s)