5

Is there any way to have more than 1 red "destructive button" in an iPhone app's UIActionSheet?

I need to have different clear options, in the same action sheet, one where it deletes everything and one where it deletes less, so both need to be red.

Jonathan.
  • 53,997
  • 54
  • 186
  • 290
  • 2
    Rather than try to modify UIActionSheet, I'd rethink your interface design here. Having more than one destructive option in a list is confusing and goes against the platform standards. Perhaps you could make your clearing actions easily undoable, making them nondestructive. – Brad Larson Dec 06 '10 at 00:00
  • It's a clear history button so it shouldn't be undoable. I guess I could have one action sheet with the 2 options as non destructive and then ask for confirmation with another action sheet with one destructive button. But it's not like the user is ressetting the phone itself. – Jonathan. Dec 06 '10 at 08:15

4 Answers4

2

The poster of this question seemed to manage what you want...if i understand you correctly.

Is using subviews in Alert undocumented

EDIT:

I swear I searched for UIActionSheet the first time. Really. http://www.nearinfinity.com/blogs/andrew_homeyer/display_a_custom_uiview_like_a.html

Community
  • 1
  • 1
griotspeak
  • 13,022
  • 13
  • 43
  • 54
2

I just created a simple customizable replacement for UIActionSheet for iPhone to use in a similar case. It does not use the standard appearance, but this can be changed. Probably it is of any use to you.

https://github.com/4marcus/WMActionSheet

marcus
  • 2,521
  • 1
  • 23
  • 32
1

There's no supported way of doing this on the standard UIActionSheet. You could build your own "action sheet" replacement using gradient buttons like the ones here:

http://iphonedevelopment.blogspot.com/2010/05/gradient-buttons-yet-again.html

I would expect someone to have created a more customizable lookalike action sheet replacement, but I don't know of one off the top of my head.

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
0

I tried this before I saw griotspeak updated answer:

SEL getTitle = NSSelectorFromString(@"title");
SEL getBackground = NSSelectorFromString(@"background");
SEL setBackground = NSSelectorFromString(@"setBackgroundImage:");
SEL setTitleColor = NSSelectorFromString(@"setTitleColor:");
UIImage *redImage;
UIColor *titleColor;
UIColor *shadowColor;
for (NSObject *object in [action subviews]) {
    if ([[NSString stringWithFormat:@"%@", [object class]] isEqualToString:@"UIThreePartButton"]) {
        if ([[object performSelector:getTitle] isEqualToString:@"Clear all"]) {
            redImage = [object performSelector:getBackground];
            titleColor = [object performSelector:@selector(titleColor)];
            shadowColor = [object performSelector:@selector(shadowColorForState:) withObject:0];
            shadowOffset = [object performSelector:@selector(shadowOffset)];
        }
        if ([[object performSelector:getTitle] isEqualToString:@"Clear all except this month"]) {
            [object performSelector:setBackground withObject:redImage];
            [object performSelector:setTitleColor withObject:titleColor];
            [object performSelector:@selector(setShadowColor:) withObject:shadowColor];
            //[object performSelector:@selector(setShadowOffset:) withObject:CGSizeMake(-2.5,0)];
        }
    }

}

(I use NSSelectorFromString rather than a @selector() because it means it's not really using undocumented things, kind of, if you get what I mean)

It's not entirely documented but I don't think I've used undocumented methods. Basically what it does is take the red background from the destructive background and apply it to the other button named "Cancel".

So that should Apple change the color of the destructive button, so will the non-destructive-destructive change as well without needing an update. Although it doesn't use particularly Apple-safe methods.

I'm having a little bit of trouble with the commented-out line above, if you can help please answer here: performSelector:withObject:, but not with an object

Community
  • 1
  • 1
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
  • 3
    No, do not do this. You are still using undocumented methods to screw around with the internal structure of a view that Apple does not let you modify. Even if Apple does not catch this during the review process, you are opening yourself up to having this application break during any future OS updates. – Brad Larson Dec 05 '10 at 23:57
  • Logically this is better because should Apple update it the 2nd destructive button will update automatically as well. If I make my own action sheet based on the current colours and apple update theirs then neither of the buttons will look correct. – Jonathan. Dec 06 '10 at 08:17
  • A counter point to that point would be that they might change the structure of the actionsheet just as easily as the style and then it would break. It is the wildcard element Brad is arguing against. But if it works now, apple approves it, and you don't mind the possibility of an update breaking it, it isn't much of an issue. – griotspeak Dec 06 '10 at 20:42