UIAlertController dismisses the TVOS using the menu button

Does anyone know how to enable the UIAlertViewController that was submitted for rejection by pressing the Menu button on tvOS?

The Settings app on Apple TV 4 has this behavior, but it doesn’t work by default in my application. I use the following code to create actions that a user can take, but would like to let him choose nothing and return by pressing the "Menu" button on the remote control.

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.

+5
source share
3 answers

@ion: Your answer led me to the correct answer.

You really need to add one action with the UIAlertActionStyleCancel style for the menu button to work properly and exit the UIAlertViewController. In order for the "Cancel" action to be hidden from the parameters (without a button, as in the "Settings" application), simply set its "Title" and "Handler" to zero:

 [alert addAction:[UIAlertAction actionWithTitle:nil style:UIAlertActionStyleCancel handler:nil]]; 
+9
source

You must have at least one UIAlertAction in the UIAlertActionStyleCancel style controller for the menu button to work as you expect.

+9
source

In Swift:

 alertController.addAction(UIAlertAction(title: nil, style: .cancel, handler: nil)) 
0
source

Source: https://habr.com/ru/post/1236749/


All Articles