Wednesday, May 29, 2013

UIActionSheet firstOtherButtonIndex example in Objective C (iOS).

UIActionSheet firstOtherButtonIndex

The index of the first custom button. (read-only)

@property(nonatomic, readonly) NSInteger firstOtherButtonIndex

Discussion of [UIActionSheet firstOtherButtonIndex]
Button indices start at 0. The default value of this property is -1, which indicates that there are no other custom buttons.

UIActionSheet firstOtherButtonIndex example.
NSURL* anURL = ... // some URL (this is only as an example on using out-of-scope variables  in blocks)
[OHActionSheet showSheetInView:yourView
                         title:@"Open this URL?"
             cancelButtonTitle:@"Cancel"
        destructiveButtonTitle:nil
             otherButtonTitles:[NSArray arrayWithObjects:@"Open",@"Bookmark",nil]
                    completion:^(OHActionSheet* sheet,NSInteger buttonIndex) {
   if (buttonIndex == sheet.cancelButtonIndex) {
     NSLog(@"You cancelled");
   } else {
     NSLog(@"You choosed button %d",buttonIndex);
     switch (buttonIndex-sheet.firstOtherButtonIndex) {
       case 0: // Open
         // here you can access the anURL variable even if this code is executed asynchrously, thanks to the magic of blocks!
         [[UIApplication sharedApplication] openURL:anURL];
         break;
       case 1: // Bookmark
       default:
         // Here you can even embed another OHAlertView for example
         [OHAlertView showAlertWithTitle:@"Wooops"
                                 message:@"This feature is not available yet, sorry!"
                            cancelButton:@"Damn"
                            otherButtons:nil
                          onButtonTapped:nil]; // no need for a completion block here
         break;
     } // switch
   }
 }];

Example of [UIActionSheet firstOtherButtonIndex].
- (void)actionSheet2:(UIActionSheet *)actionSheet2 clickedButtonAtIndex:(NSInteger)buttonIndex {

if (buttonIndex == actionSheet2.firstOtherButtonIndex + 0) {

if (_picker == nil) {
[DSBezelActivityView newActivityViewForView:self.navigationController.navigationBar.superview withLabel:@"Loading Image Picker..." width:160];
[_queue addOperationWithBlock: ^{
self.picker = [[UIImagePickerController alloc] init];
_picker.delegate = self;
_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
_picker.allowsEditing = NO;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[DSBezelActivityView removeViewAnimated:YES];
[self.navigationController presentModalViewController:_picker animated:YES];
}];
}];
} else {
[self.navigationController presentModalViewController:_picker animated:YES];
}


} else if (buttonIndex == actionSheet2.firstOtherButtonIndex + 1) {

if (_picker == nil) {
[DSBezelActivityView newActivityViewForView:self.navigationController.navigationBar.superview withLabel:@"Loading Image Picker..." width:160];
[_queue addOperationWithBlock: ^{
self.picker = [[UIImagePickerController alloc] init];
_picker.delegate = self;
_picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
_picker.allowsEditing = NO;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[DSBezelActivityView removeViewAnimated:YES];
[self.navigationController presentModalViewController:_picker animated:YES];
}];
}];
} else {
[self.navigationController presentModalViewController:_picker animated:YES];
}
}

}

UIActionSheet firstOtherButtonIndex example.
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"buttonIndex: %d, cancelButtonIndex: %d, firstOtherButtonIndex: %d",
          buttonIndex,
          actionSheet.cancelButtonIndex,
          actionSheet.firstOtherButtonIndex);
}

End of UIActionSheet firstOtherButtonIndex example article.