Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

newtoiphonesdk

macrumors 6502a
Original poster
Jul 30, 2010
567
2
I am using this as a bit of a blueprint for setting up a block based action sheet. My actions for the action sheet are Cancel, Download, and Stream. I changed the BlockBasedActionSheet.h to:
Code:
@interface BlockBasedActionSheet : UIActionSheet<UIActionSheetDelegate> {
}

@property (copy) void (^cancelBlock)();
@property (copy) void (^downloadBlock)();
@property (copy) void (^streamBlock)();


- (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle downloadButtonTitle:(NSString *)downloadButtonTitle streamButtonTitle:(NSString *)streamButtonTitle cancelAction:(void (^)())cancelBlock downloadAction:(void (^)())downloadBlock streamAction:(void (^)())streamBlock;

@end
and the BlockBasedActionSheet.m to:
Code:
@implementation BlockBasedActionSheet
@synthesize cancelBlock = _cancelBlock, streamBlock = _streamBlock, downloadBlock = _downloadBlock;

- (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle downloadButtonTitle:(NSString *)downloadButtonTitle streamButtonTitle:(NSString *)streamButtonTitle cancelAction:(void (^)())cancelBlock downloadAction:(void (^)())downloadBlock streamAction:(void (^)())streamBlock
{
    self = [super initWithTitle:title delegate:self cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:nil otherButtonTitles:downloadButtonTitle, streamButtonTitle, nil];
    if (self) {
        _cancelBlock = Block_copy(cancelBlock);
        _downloadBlock = Block_copy(downloadBlock);
        _streamBlock = Block_copy(streamBlock);
    }
    return self;
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSAssert(actionSheet == self, @"Wrong Action Sheet passed");
    if (buttonIndex == 0) {
        if (self.cancelBlock) {
            self.cancelBlock();
        }
    }
    if (buttonIndex == 1) {
        if (self.downloadBlock) {
            self.downloadBlock();
        }
    }
    if (buttonIndex == 2) {
        if (self.streamBlock) {
            self.streamBlock();
        }
    }
}

@end
In my TableView didSelectRowAtIndexPath, I put the following:
Code:
  BlockBasedActionSheet *askSheet =
    [[BlockBasedActionSheet alloc] 
     initWithTitle:@"What Do You Want To Do" cancelButtonTitle:@"Cancel" downloadButtonTitle:@"Download" streamButtonTitle:@"Stream" cancelAction:^ {
                    
     }downloadAction:^ {
         
         UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
         CGRect frame = CGRectMake(0, 49, 160, 50);
         progress = [[UIProgressView alloc] initWithFrame:frame];
         cell.contentView.tag = 100;
         [cell.contentView addSubview:progress];
         RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
         NSURL *url = [NSURL URLWithString:entry.articleUrl];    
         self.nameit = entry.articleTitle;
         NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
         receivedData = [[NSMutableData alloc] initWithLength:0];
         NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
         
     }streamAction:^ {
         if (_webViewController == nil) {
             self.webViewController = [[[WebViewController alloc] initWithNibName:@"WebViewController" bundle:[NSBundle mainBundle]] autorelease];
         }
         RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
         _webViewController.entry = entry;
         [self.navigationController pushViewController:_webViewController animated:YES];
     }];
    [askSheet showInView:self.tabBarController.view];
    [askSheet release];
The Action Sheet Presents from top to bottom:
Download
Stream
Cancel

When I press Download, the Cancel action performs
Stream, the Download action performs
Cancel, the Stream Action performs

Solution:
In the .m of BlockBasedActionSheet, changed to:
Code:
if (buttonIndex == [self cancelButtonIndex]) {
        if (self.cancelBlock) {
            self.cancelBlock();
        }
    }
    if (buttonIndex == [self firstOtherButtonIndex]) {
        if (self.downloadBlock) {
            self.downloadBlock();
        }
    }
    if (buttonIndex == [self firstOtherButtonIndex] +1) {
        if (self.streamBlock) {
            self.streamBlock();
        }
    }
 
Last edited:

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,560
6,059
Edit your post and mark it as resolved if you don't have any question anymore...

(Edit > Advanced > one of the menus lets you mark it as resolved.)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.