访问 UIAlertView 的 ViewController

Access UIAlertView's ViewController

var message = new UIAlertView();
message.AlertViewStyle = UIAlertViewStyle.SecureTextInput;
message.Title = "Enter Your Password";
message.AddButton("Login");
message.CancelButtonIndex = 0;
message.BackgroundColor = UIColor.FromRGB(139, 67, 103);
message.Show();
message.Clicked += Message_Clicked;

您好。这是我创建一个 UIAlertView 的代码,其中有一个按钮和一个文本框。我正在尝试访问它的控制器,以便我可以操纵它的子视图。我该怎么做?

你不能。这个API就是deprecated。你应该使用 UIAlertController.

来自官方document :

Starting with iOS 8, UIAlertController has completed replaced UIActionSheet and UIAlertView both of which are now deprecated.

Unlike the classes it replaced, which are subclasses of UIView, UIAlertController is a subclass of UIViewController.

最简单的alert添加了TextField,如下截图所示:

显示简单警报的代码如下:

var alertController = UIAlertController.Create("Title", "Pleasr input your name", UIAlertControllerStyle.Alert);

//Add Text
alertController.AddTextField(texfield => { texfield.Placeholder = "Please Input "; });

//Add Action
alertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, alert => {
    UITextField envirnmentNameTextField = alertController.TextFields[0];
    Console.WriteLine("Your input is :" + envirnmentNameTextField.Text);
}));

// Present Alert
PresentViewController(alertController, true, null);

如果想改变消息或标题的颜色,可以使用NSMutableAttributedString来实现。

//set color to meesage NSForegroundColorAttributeName

UIStringAttributes attrTextColor = new UIStringAttributes();
attrTextColor.ForegroundColor = UIColor.Purple;

NSMutableAttributedString messageText = new NSMutableAttributedString("Please input your name");
messageText.AddAttributes(attrTextColor, new NSRange(0, 6));
alertController.SetValueForKey(messageText, new NSString("attributedMessage"));