Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertController dismiss on tvOS with menu button

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.

like image 270
Benoit Avatar asked Nov 24 '15 21:11

Benoit


3 Answers

@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]];
like image 158
Benoit Avatar answered Nov 20 '22 05:11

Benoit


In Swift:

alertController.addAction(UIAlertAction(title: nil, style: .cancel, handler: nil))
like image 44
jakedunc Avatar answered Nov 20 '22 05:11

jakedunc


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

like image 11
ion Avatar answered Nov 20 '22 03:11

ion