Forum General

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

TVOS: Issue with showing alert on tvOS Application

SreeeeSreeee INMember ✭✭✭✭✭

I am a beginner in tvOS app development. I am trying to show an alert when clicking a button in my tvOS application.

I tried the below code, but showing errors:

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
}

Screenshot:

enter image description here

Need any packages for solving these issues? I follow this blog for creating this sample project(Quiz Game App section).

Tagged:

Best Answers

  • JohnHardmanJohnHardman GBUniversity admin
    Accepted Answer

    @Sreeee said:
    In this blog, they have added that code for showing alert and it is added on the ViewController.cs file

    It's not a .cs file.

    To re-use that code, you need to translate it from Swift to C#. To start with, get rid of the "let" keywords, replace "self" by "this", add appropriate "using" lines (intellisense should identify for you which ones you need). There's still more to do after that too.

    Alternatively, you might want to look at https://docs.microsoft.com/en-us/samples/xamarin/ios-samples/tvos-tvalerts/ which will at least be in the same language as you want to use.

  • SreeeeSreeee INMember ✭✭✭✭✭
    edited June 2020 Accepted Answer

    Thanks, @JohnHardman for directing me to the right way. Finally, I am able to show different types of alerts on my app using this sample project. Posting the codes here, which may help others.

    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
    

Answers

  • JohnHardmanJohnHardman GBUniversity admin

    @Sreeee

    What file have you put that code in? Is it a .cs file? The code that you have copied is Swift, not C#. You'll need to adapt it accordingly if you are working in C# (which I assume you are if using Xamarin).

  • SreeeeSreeee INMember ✭✭✭✭✭

    @JohnHardman

    It is a .cs file, named ViewController.cs

    Have a Main.storyboard and ViewController.cs files on the project. On .storyboard file adding the controllers(buttons, label etc) and on the .cs file adding the code.

    Actually this is my first tvOS application. I am following this blog for creating the tv applications. In the Quiz Game App section of the blog, they are telling this method for showing an alert.

  • JohnHardmanJohnHardman GBUniversity admin

    @Sreeee said:
    It is a .cs file, named ViewController.cs

    @JohnHardman said:
    The code that you have copied is Swift, not C#. You'll need to adapt it accordingly if you are working in C# (which I assume you are if using Xamarin).

  • SreeeeSreeee INMember ✭✭✭✭✭

    @JohnHardman

    Could you please have a look on the below blog. Please check the Quiz Game App section.
    https://www.appcoda.com/tvos-introduction/

    In this blog, they have added that code for showing alert and it is added on the ViewController.cs file

    I am totally confused o:)

  • JohnHardmanJohnHardman GBUniversity admin
    Accepted Answer

    @Sreeee said:
    In this blog, they have added that code for showing alert and it is added on the ViewController.cs file

    It's not a .cs file.

    To re-use that code, you need to translate it from Swift to C#. To start with, get rid of the "let" keywords, replace "self" by "this", add appropriate "using" lines (intellisense should identify for you which ones you need). There's still more to do after that too.

    Alternatively, you might want to look at https://docs.microsoft.com/en-us/samples/xamarin/ios-samples/tvos-tvalerts/ which will at least be in the same language as you want to use.

  • SreeeeSreeee INMember ✭✭✭✭✭
    edited June 2020 Accepted Answer

    Thanks, @JohnHardman for directing me to the right way. Finally, I am able to show different types of alerts on my app using this sample project. Posting the codes here, which may help others.

    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
    
Sign In or Register to comment.