TVOS:在tvOS应用程序上显示警报的问题

问题描述 投票:0回答:1

我是tvOS应用程序开发的初学者。单击tvOS应用程序中的按钮时,我试图显示警报。

我尝试了以下代码,但显示了错误:

let alertController = UIAlertController(title: status, message: title, preferredStyle: .Alert) // 2
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in //3 
}
alertController.addAction(cancelAction)

let ok = UIAlertAction(title: "OK", style: .Default) { (action) in
} // 4
alertController.addAction(ok)

self.presentViewController(alertController, animated: true) { // 5
}

屏幕截图:

enter image description here

需要任何软件包来解决这些问题吗?我跟随this博客创建此示例项目(Quiz Game App部分)。

tvos
1个回答
0
投票

最后,我可以使用this示例项目在我的应用上显示不同类型的警报。在此处发布代码,可能会对其他人有所帮助。

ViewController.cs

partial void DisplayDestructiveAlert (Foundation.NSObject sender) {
    // User helper class to present alert
    AlertViewController.PresentDestructiveAlert("A Short Title is Best","The message should be a short, complete sentence.","Delete",this, (ok) => {
        Console.WriteLine("Destructive Alert: The user selected {0}",ok);
    });
}

partial void DisplayOkCancelAlert (Foundation.NSObject sender) {
    // User helper class to present alert
    AlertViewController.PresentOKCancelAlert("A Short Title is Best","The message should be a short, complete sentence.",this, (ok) => {
        Console.WriteLine("OK/Cancel Alert: The user selected {0}",ok);
    });
}

partial void DisplaySimpleAlert (Foundation.NSObject sender) {
    // User helper class to present alert
    AlertViewController.PresentOKAlert("A Short Title is Best","The message should be a short, complete sentence.",this);
}

partial void DisplayTextInputAlert (Foundation.NSObject sender) {
    // User helper class to present alert
    AlertViewController.PresentTextInputAlert("A Short Title is Best","The message should be a short, complete sentence.","placeholder", "", this, (ok, text) => {
        Console.WriteLine("Text Input Alert: The user selected {0} and entered `{1}`",ok,text);
    });
}

AlertViewController.cs

#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 PresentDestructiveAlert(string title, string description, string destructiveAction, 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(destructiveAction,UIAlertActionStyle.Destructive,(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;
}
#endregion

#region Delegates
public delegate void AlertOKCancelDelegate(bool OK);
public delegate void AlertTextInputDelegate(bool OK, string text);
#endregion
© www.soinside.com 2019 - 2024. All rights reserved.