I need to implement a method that returns user input that is received from UIAlertView.
This is what I did so far:
It returns initial value of string, no matter what the user enters text. Any ideas?
private string GetUserInput()
{
string userInput = string.Empty;
UIAlertView alert = new UIAlertView();
alert.Title = "What is your favourite movie?";
alert.AddButton("OK");
alert.AddButton("Cancel");
alert.AlertViewStyle = UIAlertViewStyle.PlainTextInput;
alert.Clicked += (object s, UIButtonEventArgs ev) =>
{
if (ev.ButtonIndex != 0) return;
userInput = alert.GetTextField(0).Text;
};
alert.Show();
return userInput;
}
SOLUTION:
I found the solution. I put it below in case of somebody needs it.
string userInput = await Methods.ShowAlert("What is your favourite movie?", "", "OK", "Cancel");
ShowAlert Method:
public static Task<string> ShowAlert(string title, string message,params string[] buttons)
{
var tcs = new TaskCompletionSource<string>();
var alert = new UIAlertView
{
Title = title,
Message = message,
AlertViewStyle = UIAlertViewStyle.PlainTextInput
};
foreach (var button in buttons)
alert.AddButton(button);
alert.Clicked += (s, e) =>
{
if (e.ButtonIndex != 0) return;
tcs.TrySetResult(alert.GetTextField(0).Text);
};
alert.Show();
return tcs.Task;
}