Wednesday, May 29, 2013

UIActionSheet numberOfButtons example in Objective C (iOS).

UIActionSheet numberOfButtons

The number of buttons on the action sheet. (read-only)

@property(nonatomic, readonly) NSInteger numberOfButtons

UIActionSheet numberOfButtons example.
-(void)populateSheetAndShow:(NSArray *) accountsArray {
    NSMutableArray *buttonsArray = [NSMutableArray array];
    [accountsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            [buttonsArray addObject:((ACAccount*)obj).username];
        }];


        NSLog(@"%@", buttonsArray);

        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
        for( NSString *title in buttonsArray)
            [actionSheet addButtonWithTitle:title];
        [actionSheet addButtonWithTitle:@"Cancel"];
        actionSheet.cancelButtonIndex = actionSheet.numberOfButtons-1;
        [actionSheet showInView:self.view];
}

Example of [UIActionSheet numberOfButtons].
[actionSheet addButtonWithTitle:@"Cancel"];
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1;

UIActionSheet numberOfButtons example.
UIActionSheet *sheet = [[UIActionSheet alloc]
                            initWithTitle:@""
                            delegate:self
                            cancelButtonTitle:nil
                            destructiveButtonTitle:nil
                            otherButtonTitles:nil];

    [sheet addButtonWithTitle:@"Email"];
    [sheet addButtonWithTitle:@"Tweet"];
    [sheet addButtonWithTitle:@"Cancel"];

    sheet.cancelButtonIndex = sheet.numberOfButtons-1;

    [sheet showFromRect:self.view.bounds inView:self.view animated:YES];
    [sheet release];

End of UIActionSheet numberOfButtons example article.