Does anyone know how to enable a UIAlertViewController which has been presented to be dismissed by clicking the "Menu" button on tvOS?
The Settings app on the Apple TV 4 features that behavior but it doesn't work by default in my app. I use the following code to create the actions the user can take, but would like to allow him not to chose anything and go back by pressing the "Menu" button on the remote.
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Alert"
message:@"Please make a choice"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action1 = [UIAlertAction actionWithTitle:@"Option 1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
[alert addAction:action1];
UIAlertAction* action2 = [UIAlertAction actionWithTitle:@"Option 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
[alert addAction:action2];
[self presentViewController:alert animated:YES completion:nil];
Thanks in advance.
@ion: Your answer led me to the right answer.
You indeed need to add one action with style UIAlertActionStyleCancel to have the menu button to work as expected and exit the UIAlertViewController. To have this Cancel action hidden from the options (no button, like in the Settings app), just set its Title and Handler to nil:
[alert addAction:[UIAlertAction actionWithTitle:nil style:UIAlertActionStyleCancel handler:nil]];
In Swift:
alertController.addAction(UIAlertAction(title: nil, style: .cancel, handler: nil))
You must have at least one UIAlertAction in the controller of style UIAlertActionStyleCancel for the menu button to work as you expect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With