TVOS:在 tvOS 应用程序上显示警报时出现问题

TVOS: Issue with showing alert on tvOS Application

我是 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
}

截图:

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

最后,我可以使用 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);
    });
}

警报ViewController.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