Xamarin. iOS неправильно отображает кнопки в оповещении - PullRequest
0 голосов
/ 09 марта 2020

enter image description here

Я не получаю свою кнопку OK при предупреждении. Разве это не код для отображения предупреждения кнопки ОК?

Что мне нужно изменить?

    public void Alert (List<string> listToAlert, string heading)
            {
                    StringBuilder stringBuilder = new StringBuilder ();
                    for (int i = 0; i < listToAlert.Count; i++) {
                            stringBuilder.Append (listToAlert [i]);
                            stringBuilder.Append ("\r\n");
                    }
                    UIButton okayButton = new UIButton();
                    var okAlertController = UIAlertController.Create (heading, stringBuilder.ToString (), UIAlertControllerStyle.Alert);

                    okayButton.TouchUpInside += (sender, e) => {    
                            //Add Action
                            okAlertController.AddAction (UIAlertAction.Create ("Cancel", UIAlertActionStyle.Cancel, null));
                    };
                    _rootVC.PresentViewController (okAlertController, true, null);
            }

Ответы [ 2 ]

0 голосов
/ 10 марта 2020

Вот рабочее решение - вышеприведенное решение @Ricardo Romo не компилируется. Скорее всего, речь идет о предположении относительно класса содержащегося типа. PresentViewController предполагает, что вызывающий класс является ViewController. В моем случае и во многих случаях, и в моем, это не всегда так. Я звоню из appdelegate. enter image description here

public class AlertViewController
    {
            #region Static Methods
            public static UIAlertController PresentOKAlert (string title, string description, UIViewController controller)
            {
                    // No, inform the user that they must create a home first
                    UIAlertController alert = UIAlertController.Create (title, description, UIAlertControllerStyle.Alert);

                    // Configure the alert
                    alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, (action) => { }));

                    // Display the alert
                    controller.PresentViewController (alert, true, null);

                    // Return created controller
                    return alert;
            }
            public static UIAlertController PresentOKCancelAlert (string title, string description, UIViewController controller, AlertOKCancelDelegate action)
            {
                    // No, inform the user that they must create a home first
                    UIAlertController alert = UIAlertController.Create (title, description, UIAlertControllerStyle.Alert);

                    // Add cancel button
                    alert.AddAction (UIAlertAction.Create ("Cancel", UIAlertActionStyle.Cancel, (actionCancel) => {
                            // Any action?
                            if (action != null) {
                                    action (false);
                            }
                    }));

                    // Add ok button
                    alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, (actionOK) => {
                            // Any action?
                            if (action != null) {
                                    action (true);
                            }
                    }));

                    // Display the alert
                    controller.PresentViewController (alert, true, null);

                    // Return created controller
                    return alert;
            }
            public static UIAlertController PresentTextInputAlert (string title, string description, string placeholder, string text, UIViewController controller, AlertTextInputDelegate action)
            {
                    // No, inform the user that they must create a home first
                    UIAlertController alert = UIAlertController.Create (title, description, UIAlertControllerStyle.Alert);
                    UITextField field = null;

                    // Add and configure text field
                    alert.AddTextField ((textField) => {
                            // Save the field
                            field = textField;

                            // Initialize field
                            field.Placeholder = placeholder;
                            field.Text = text;
                            field.AutocorrectionType = UITextAutocorrectionType.No;
                            field.KeyboardType = UIKeyboardType.Default;
                            field.ReturnKeyType = UIReturnKeyType.Done;
                            field.ClearButtonMode = UITextFieldViewMode.WhileEditing;

                    });

                    // Add cancel button
                    alert.AddAction (UIAlertAction.Create ("Cancel", UIAlertActionStyle.Cancel, (actionCancel) => {
                            // Any action?
                            if (action != null) {
                                    action (false, "");
                            }
                    }));

                    // Add ok button
                    alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, (actionOK) => {
                            // Any action?
                            if (action != null && field != null) {
                                    action (true, field.Text);
                            }
                    }));

                    // Display the alert
                    controller.PresentViewController (alert, true, null);

                    // Return created controller
                    return alert;
            }
            public static void Alert (List<string> listToAlert, string heading, UIViewController controller)
            {
                    StringBuilder stringBuilder = new StringBuilder ();
                    for (int i = 0; i < listToAlert.Count; i++) {
                            stringBuilder.Append (listToAlert [i]);
                            stringBuilder.Append ("\r\n");
                    }
                    AlertViewController.PresentOKAlert (heading, stringBuilder.ToString (), controller);
            }
            #endregion
            #region Delegates
            public delegate void AlertOKCancelDelegate (bool OK);
            public delegate void AlertTextInputDelegate (bool OK, string text);
            #endregion
    }
0 голосов
/ 09 марта 2020

Вы добавляете действие до тех пор, пока пользователь не нажмет кнопку ОК, вы должны добавить его после создания контроллера предупреждений пользовательского интерфейса

var okAlertController = UIAlertController.Create ("Title", "The      message", UIAlertControllerStyle.Alert);

    //Ads Action
    okAlertController.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, null));

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

Посмотрите в документации, чтобы увидеть больше примеров https://docs.microsoft.com/en-us/xamarin/ios/user-interface/controls/alerts

...