UIActionSheet addButtonWithTitle: doesn't add buttons in the right order

前端 未结 3 884
后悔当初
后悔当初 2021-01-04 21:27

I\'ve subclassed UIActionSheet, and in the -init method, I have to add the buttons individually after calling the super init (can\'t p

3条回答
  •  青春惊慌失措
    2021-01-04 21:50

    The earlier answers cause the destructive button to be placed at the bottom, which is not in accordance with the HIG, and which is also very confusing for the user. The destructive button should be at the top, the cancel on the bottom, and the others in the middle.

    The following orders them correctly:

    sheetView         = [[UIActionSheet alloc] initWithTitle:title delegate:self
                                           cancelButtonTitle:nil destructiveButtonTitle:destructiveTitle otherButtonTitles:firstOtherTitle, nil];
    if (otherTitlesList) {
        for (NSString *otherTitle; (otherTitle = va_arg(otherTitlesList, id));)
            [sheetView addButtonWithTitle:otherTitle];
        va_end(otherTitlesList);
    }
    if (cancelTitle)
        sheetView.cancelButtonIndex      = [sheetView addButtonWithTitle:cancelTitle];
    

    See https://github.com/Lyndir/Pearl/blob/master/Pearl-UIKit/PearlSheet.m for an implementation (a UIActionSheet wrapper with a block-based API).

提交回复
热议问题