/// <summary> /// Shows an alert with Textfields. /// For iOS 7 or before, an alert with a Textfield will be shown. /// </summary> /// <param name="title">The title of the alert.</param> /// <param name="message">Message to show to user.</param> /// <param name="fromViewController">From which view the alert will be presented.</param> /// <param name="okTitle">Title for Ok button.</param> /// <param name="placeholders"> /// Placeholders to be shown in textfields. A Textfield will be added for each placeholder /// If null or an empty array is passed, a simple alert will be shown. /// </param> /// <param name="result"> /// Action to be done when user press one button of alert. /// Will return all inputs from Textfields in order that were shown or null if Cancel button was tapped. /// </param> public static void ShowMessage(string title, string message, UIViewController fromViewController, string okTitle, string [] placeholders, UIAlertControllerTextFieldResultHandler result) { if (string.IsNullOrWhiteSpace(okTitle)) { okTitle = "Ok"; } if (placeholders == null || placeholders.Length == 0) { ShowMessage(title, message, fromViewController, okTitle); return; } var alert = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert); foreach (var placeholder in placeholders) { alert.AddTextField((textField) => { textField.Placeholder = placeholder; textField.TextAlignment = UITextAlignment.Center; }); } alert.AddAction(UIAlertAction.Create(okTitle, UIAlertActionStyle.Default, (obj) => { var textfields = alert.TextFields; var inputs = new string [textfields.Length]; for (int i = 0; i < textfields.Length; i++) { inputs [i] = textfields [i].Text; } result?.Invoke(false, inputs); })); alert.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, (obj) => { result?.Invoke(true, null); })); fromViewController.PresentViewController(alert, true, null); }
/// <summary> /// Shows an alert with Textfields. /// For iOS 7 or before, an alert with a Textfield will be shown. /// </summary> /// <param name="title">The title of the alert.</param> /// <param name="message">Message to show to user.</param> /// <param name="fromViewController">From which view the alert will be presented.</param> /// <param name="okTitle">Title for Ok button.</param> /// <param name="placeholders"> /// Placeholders to be shown in textfields. A Textfield will be added for each placeholder /// If null or an empty array is passed, a simple alert will be shown. /// </param> /// <param name="result"> /// Action to be done when user press one button of alert. /// Will return all inputs from Textfields in order that were shown or null if Cancel button was tapped. /// </param> public static void ShowMessage(string title, string message, UIViewController fromViewController, string okTitle, string [] placeholders, UIAlertControllerTextFieldResultHandler result) { if (string.IsNullOrWhiteSpace(okTitle)) { okTitle = "Ok"; } if (placeholders == null || placeholders.Length == 0) { ShowMessage(title, message, fromViewController, okTitle); return; } if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0)) { var alert = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert); foreach (var placeholder in placeholders) { alert.AddTextField((textField) => { textField.Placeholder = placeholder; textField.TextAlignment = UITextAlignment.Center; }); } alert.AddAction(UIAlertAction.Create(okTitle, UIAlertActionStyle.Default, (obj) => { var textfields = alert.TextFields; var inputs = new string [textfields.Length]; for (int i = 0; i < textfields.Length; i++) { inputs [i] = textfields [i].Text; } result?.Invoke(false, inputs); })); alert.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, (obj) => { result?.Invoke(true, null); })); fromViewController.PresentViewController(alert, true, null); } else { var alert = new UIAlertView(title, message, null, "Cancel", okTitle) { AlertViewStyle = UIAlertViewStyle.PlainTextInput }; alert.Dismissed += (sender, e) => { var buttonIndex = (nint)e.ButtonIndex; if (buttonIndex == 0) { result?.Invoke(true, null); } else { var value = (sender as UIAlertView).GetTextField(0).Text; result?.Invoke(false, new [] { value }); } }; alert.Show(); } }