【问题标题】:Use Block Based Action Sheet使用基于块的操作表
【发布时间】:2012-03-13 04:46:40
【问题描述】:

我使用this 作为设置基于块的操作表的蓝图。我对操作表的操作是取消、下载和流式传输。我将 BlockBasedActionSheet.h 更改为:

 @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

和 BlockBasedActionSheet.m 到:

@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

在我的 TableView didSelectRowAtIndexPath 中,我输入了以下内容:

    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];

操作表从上到下呈现: 下载 溪流 取消

当我按下下载时,取消动作执行; Stream,下载动作执行; 取消,Stream Action 执行

我做错了什么导致它出现故障?

【问题讨论】:

    标签: iphone uitableview objective-c-blocks uiactionsheet


    【解决方案1】:

    试试这个:

    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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-27
      相关资源
      最近更新 更多