iostwitteruiactionsheetacaccount

Dynamic UIActionSheet 'otherButtonTitles:'


I'm trying to create something to list all of a user's Twitter accounts connected to the device in a UIActionSheet. For instance, I have three Twitter accounts on my device. I'd like the action sheet to list my accounts with a cancel button. Currently, my function looks like this:

- (void)showAlertViewForAccounts:(NSArray*)accounts {
    accounts = _.arrayMap(accounts, ^id(ACAccount *account) {
        return account.username;
    });

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose an account:"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];
    for (NSString *username in accounts) {
        [actionSheet addButtonWithTitle:username];
    }    


    [actionSheet showInView:[[[[[UIApplication sharedApplication] delegate] window] rootViewController] view]];
}

My issue is that the cancel button is not displayed in a separate 'section' of the action sheet.

Is there anyway that I can A.) convert the accounts array to a va_list to be passed in as a parameter of the UIActionSheet init... method, or B.) specify that the cancel button should be displayed in a separate 'section'?


Solution

  • Add the Cancel button after the others:

    - (void)showAlertViewForAccounts:(NSArray*)accounts {
        accounts = _.arrayMap(accounts, ^id(ACAccount *account) {
            return account.username;
        });
    
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose an account:"
                                                                 delegate:self
                                                        cancelButtonTitle:nil
                                                   destructiveButtonTitle:nil
                                                        otherButtonTitles:nil];
        for (NSString *username in accounts) {
            [actionSheet addButtonWithTitle:username];
        }    
    
        actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];  
    
        [actionSheet showInView:[[[[[UIApplication sharedApplication] delegate] window] rootViewController] view]];
    }