I have a legacy C# class that extends UIAlertView
for prompting the user to enter an IP address. It also will persist the IP address and load it as the default entry value.
The original code hacked the iOS4.x UIAlertView
to add a text field and it had several layout problems. I converted the code to use the iOS5 UIAlertView
's AlertViewStyle
setting and everything worked fine.
I was asked to enable the Done key on the popup keyboard so that it could be a shortcut to the Connect button. I added code as follows to the class's constructor:
public AddressAlertView()
{
AddButton("Cancel");
AddButton("Connect");
AlertViewStyle = UIAlertViewStyle.PlainTextInput;
// NEW - get input text field, enable Done on the keyboard and
// allow Done to be the same as the Connect button
UITextField fld = GetTextField(0);
fld.ReturnKeyType = UIReturnKeyType.Done;
fld.ShouldReturn = (textField) =>
{
DismissWithClickedButtonIndex(1, true);
return textField.ResignFirstResponder();
};
// *** end of new code ***
// restore saved IP address
string savedAddress = NSUserDefault.StandardUserDefaults.StringForKey(AddressKey);
if (savedAddress != null)
fld.Text = savedAddress;
}
The class gets used like so:
....
AddressAlertView addrPrompt = new AddressAlertView();
addrPrompt.Title = "IP Address";
addrPrompt.Message = "Enter address";
addrPrompt.Dismissed += (sender, e) =>
{
if (e.ButtonIndex == 1)
{
// use IP address...
....
}
};
addrPrompt.Show();
....
Now the problem. When running this code, the AddressAlertView
shows the popup dialog correctly and everything works as before. However, if I tap Done on the keyboard, the app bombs out in UIAlertView
.
I tried moving the new code to the public override void LayoutSubviews()
method, but it still crashes.
Any ideas?