Wednesday, May 29, 2013

UIActionSheet initWithTitle delegate cancelButtonTitle destructiveButtonTitle otherButtonTitles example in Objective C (iOS).

UIActionSheet initWithTitle delegate cancelButtonTitle destructiveButtonTitle otherButtonTitles

Initializes the action sheet using the specified starting parameters.

- (id)initWithTitle:(NSString *)title delegate:(id < UIActionSheetDelegate >)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...

Parameters of [UIActionSheet initWithTitle delegate cancelButtonTitle destructiveButtonTitle otherButtonTitles]
title
A string to display in the title area of the action sheet. Pass nil if you do not want to display any text in the title area.
delegate
The receiver’s delegate object. Although this parameter may be nil, the delegate is used to respond to taps in the action sheet and should usually be provided.
cancelButtonTitle
The title of the cancel button. This button is added to the action sheet automatically and assigned an appropriate index, which is available from the cancelButtonIndex property. This button is displayed in black to indicate that it represents the cancel action. Specify nil if you do not want a cancel button or are presenting the action sheet on an iPad.[UIActionSheet initWithTitle delegate cancelButtonTitle destructiveButtonTitle otherButtonTitles]
destructiveButtonTitle
The title of the destructive button. This button is added to the action sheet automatically and assigned an appropriate index, which is available from the destructiveButtonIndex property. This button is displayed in red to indicate that it represents a destructive behavior. Specify nil if you do not want a destructive button.
otherButtonTitles, ...
The titles of any additional buttons you want to add. This parameter consists of a nil-terminated, comma-separated list of strings. For example, to specify two additional buttons, you could specify the value @"Button 1", @"Button 2", nil.

Return Value of [UIActionSheet initWithTitle delegate cancelButtonTitle destructiveButtonTitle otherButtonTitles]
A newly initialized action sheet.

Discussion of [UIActionSheet initWithTitle delegate cancelButtonTitle destructiveButtonTitle otherButtonTitles]
The action sheet automatically sets the appearance of the destructive and cancel buttons. If the action sheet contains only one button, it does not apply the custom colors associated with the destructive and cancel buttons.

UIActionSheet initWithTitle delegate cancelButtonTitle destructiveButtonTitle otherButtonTitles example.
UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString
                              delegate: self
                              cancelButtonTitle: nil
                              destructiveButtonTitle: nil
                              otherButtonTitles: nil];

Example of [UIActionSheet initWithTitle delegate cancelButtonTitle destructiveButtonTitle otherButtonTitles].
- (void)showActionSheetWithButtons:(NSArray *)buttons withTitle:(NSString *)title {

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle: title
                                                             delegate: self
                                                    cancelButtonTitle: nil
                                               destructiveButtonTitle: nil
                                                    otherButtonTitles: nil];

    for (NSString *title in buttons) {
        [actionSheet addButtonWithTitle: title];
    }

    [actionSheet addButtonWithTitle: @"Cancel"];
    [actionSheet setCancelButtonIndex: [buttons count]];
    [actionSheet showInView:self.view];
}

UIActionSheet initWithTitle delegate cancelButtonTitle destructiveButtonTitle otherButtonTitles example.
NSArray *array = [[NSArray alloc] initWithObjects:
                      [NSString stringWithString:@"1st Button"],
                      [NSString stringWithString:@"2nd Button"],
                      [NSString stringWithString:@"3rd Button"],
                      [NSString stringWithString:@"4th Button"],
                      nil];

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title Here"
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];

    for (int i = 0; i < [array count]--; i++) {

        [actionSheet addButtonWithTitle:[array objectAtIndex:i]];

    }

    [actionSheet addButtonWithTitle:@"Cancel"];
    actionSheet.cancelButtonIndex = array.count;

    [actionSheet showInView:self.view];

End of UIActionSheet initWithTitle delegate cancelButtonTitle destructiveButtonTitle otherButtonTitles example article.