Wednesday, May 29, 2013

UIActionSheet addButtonWithTitle example in Objective C (iOS).

UIActionSheet addButtonWithTitle

Adds a custom button to the action sheet.

- (NSInteger)addButtonWithTitle:(NSString *)title

Parameters
title
The title of the new button.

Return Value of [UIActionSheet addButtonWithTitle]
The index of the new button. Button indices start at 0 and increase in the order they are added.

UIActionSheet addButtonWithTitle example.
if (self = [super initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTile:nil otherButtonTitles:nil]) {
  if (firstButton) {
    id buttonTitle;
    int idx = 0;
    va_list argList;
    va_start(argList, firstButtton);
    while (buttonTitle = va_arg(argList, id)) {
      [self addButtonWithTitle:buttonTitle]
      idx++;
    }
    va_end(argList);
    [self addButtonWithTitle:cancel];
    [self addButtonWithTitle:destroy];
    self.cancelButtonIndex = idx++;
    self.destructiveButtonIndex = idx++;
  }
}
return self;

Example of [UIActionSheet addButtonWithTitle].
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];

UIActionSheet addButtonWithTitle example.
UIActionSheet* sheet = [[[UIActionSheet alloc] init] autorelease];
sheet.title = @"Illustrations";
sheet.delegate = self;
[sheet addButtonWithTitle:@"ABC"];
[sheet addButtonWithTitle:@"XYZ"];
if (condition)
    [sheet addButtonWithTitle:@"LMNOP"];
sheet.cancelButtonIndex = [sheet addButtonWithTitle:@"Cancel"];

End of UIActionSheet addButtonWithTitle example article.