change position of cancel button in UIAlertView?
Asked Answered
B

2

12

I noticed that when I delete an app from my iPhone home screen, the alert view that appears shows a Delete button on the left and Cancel on the right. However, when I build a delete function within my app using UIAlertView, the buttons only seem to display with Cancel on the left and Delete on the right.

I'd like my app to be consistent with the OS, but I can't figure out how to make the Cancel button appear first. Does anyone know?

UIAlertView *alert = [[UIAlertView alloc] 
                      initWithTitle:@"Delete Song" 
                      message:@"Are you sure you want to delete this song? This will permanently remove it from your database." 
                      delegate:self 
                      cancelButtonTitle:@"Cancel" 
                      otherButtonTitles:@"Delete", nil];

I tried setting alert.cancelButtonIndex = 1, but that had no effect.

Br answered 16/12, 2010 at 1:29 Comment(0)
N
4

A possible reason Apple used an alert view on the home screen was because it once asked users to rate the apps they were removing (not anymore). They likely made the Cancel button the lighter-colored one because this was considered a destructive action (deletes an app and its data).

I guess you could reverse the titles (cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil) and handle clicks on those buttons the other way around (not sure if Apple did the same). That would be a little awkward though; how about using an action sheet instead?

Newcomen answered 16/12, 2010 at 1:38 Comment(4)
Hmm, do you know what's standard for other parts of the OS? I can't think of any other examples of where to look for this. Anyway, if deleting an app from the home screen uses a non-standard setup, then I don't need to match that.Br
@arlomedia: I've not seen any other delete alerts around. Both the SMS and calendar alerts place their cancel (Close) buttons at the left, just like the SDK. AFAIK, anywhere else deleting something is involved (photos, contacts, calendar events, notes, iPod playlists) uses action sheets.Newcomen
Okay, I'll just leave it as is then. Thanks for the info.Br
The [developer.apple.com/library/ios/#documentation/userexperience/… specifies: "In a two-button alert that proposes a potentially risky action, the button that cancels the action should be on the right and light-colored. In a two-button alert that proposes a benign action that people are likely to want, the button that cancels the action should be on the left and dark-colored". I guess this is the reason that the order/colour of buttons varies.Steamtight
B
53

Ah, I just figured out how to change this. The cancelButtonTitle argument is optional, so you can add a custom button in whatever position you want and then designate that as the cancel button, like this:

UIAlertView *alert = [[UIAlertView alloc] 
                      initWithTitle:@"Delete Song" 
                      message:@"Are you sure you want to delete this song? This will permanently remove it from your database." 
                      delegate:self 
                      cancelButtonTitle:nil
                      otherButtonTitles:@"Delete", @"Cancel", nil];
alert.cancelButtonIndex = 1;

That puts the Delete button the left and the Cancel button on the right and highlights the Cancel button.

Br answered 16/12, 2010 at 5:35 Comment(2)
I thought I was going nuts until I saw this. I tried using @"NO" for cancel button and @"YES" as other button titles but buttonIndex 0 was the cancel button. I really can't see that as anything other than a bug on Apples part, but who am I to question what I can't fathom eh? This approach works perfectly though, Thanks!Bighorn
I'm finding that this no longer works on iOS 8.3 (in the Simulator), but works fine on iOS 7.1. This despite the fact that some Apple alerts still do put "Cancel" on the right, even in 8.3. (I wonder how they do it!)Botanist
N
4

A possible reason Apple used an alert view on the home screen was because it once asked users to rate the apps they were removing (not anymore). They likely made the Cancel button the lighter-colored one because this was considered a destructive action (deletes an app and its data).

I guess you could reverse the titles (cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil) and handle clicks on those buttons the other way around (not sure if Apple did the same). That would be a little awkward though; how about using an action sheet instead?

Newcomen answered 16/12, 2010 at 1:38 Comment(4)
Hmm, do you know what's standard for other parts of the OS? I can't think of any other examples of where to look for this. Anyway, if deleting an app from the home screen uses a non-standard setup, then I don't need to match that.Br
@arlomedia: I've not seen any other delete alerts around. Both the SMS and calendar alerts place their cancel (Close) buttons at the left, just like the SDK. AFAIK, anywhere else deleting something is involved (photos, contacts, calendar events, notes, iPod playlists) uses action sheets.Newcomen
Okay, I'll just leave it as is then. Thanks for the info.Br
The [developer.apple.com/library/ios/#documentation/userexperience/… specifies: "In a two-button alert that proposes a potentially risky action, the button that cancels the action should be on the right and light-colored. In a two-button alert that proposes a benign action that people are likely to want, the button that cancels the action should be on the left and dark-colored". I guess this is the reason that the order/colour of buttons varies.Steamtight

© 2022 - 2024 — McMap. All rights reserved.