Wednesday, May 29, 2013

UIActionSheet destructiveButtonIndex example in Objective C (iOS).

UIActionSheet destructiveButtonIndex

The index number of the destructive button.

@property(nonatomic) NSInteger destructiveButtonIndex

Discussion of [UIActionSheet destructiveButtonIndex]
Button indices start at 0. The default value of this property is normally -1, which indicates that no destructive button has been set. However, a destructive button may be created and set automatically by the initWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles: method. If you use that method to create a destructive button, you should not change the value of this property.

UIActionSheet destructiveButtonIndex example.
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == actionSheet.destructiveButtonIndex)
    {
        // Do something...
    }
}

Example of [UIActionSheet destructiveButtonIndex].
-(IBAction) ChangeArrow:(id)sender{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Change Arrow"
                                                         delegate:self
                                                cancelButtonTitle:@"cancel"
                                           destructiveButtonTitle:@"Red"
                                                otherButtonTitles:@"Blue",@"Black",nil];
[actionSheet showInView:self.view];
[actionSheet release];}
- (void) actionSheet: (UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex ==[actionSheet destructiveButtonIndex]) {
    self.bar.image=[UIImage imageNamed:@"red"];

}
else if(buttonIndex == 1){
    self.bar.image=[UIImage imageNamed:@"blue"];

}
else if(buttonIndex == 2){
    self.bar.image=[UIImage imageNamed:@"dark"];}
}

UIActionSheet destructiveButtonIndex 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;

End of UIActionSheet destructiveButtonIndex example article.