0

我已经阅读了很多关于使用块的积极内容——特别是它通过消除委托调用来简化代码。我发现了在动画结束时使用块而不是委托调用的示例。块示例很简单,但我不能将示例用于 iphone 应用程序。例如,我使用委托:

。H

@protocol AWActionSheetDelegate <NSObject>

- (int)numberOfItemsInActionSheet;
- (AWActionSheetCell*)cellForActionAtIndex:(NSInteger)index;
- (void)DidTapOnItemAtIndex:(NSInteger)index;

@end

@interface AWActionSheet : UIActionSheet

   @property (nonatomic, assign)id<AWActionSheetDelegate> IconDelegate;
-(id)initwithIconSheetDelegate:(id<AWActionSheetDelegate>)delegate ItemCount:(int)cout;
@end

.m

- (void)actionForItem:(UITapGestureRecognizer*)recongizer
{

   [IconDelegate DidTapOnItemAtIndex:cell.index];

}

我用它:

 -(void)DidTapOnItemAtIndex:(NSInteger)index
{
    NSLog(@"tap on %d",index);

}

如何使用块不使用委托我可以获得索引,您能否提供建议,如果提供好的块类别来完成效果非常好。我不想使用委托传递索引,我只想使用块来获取索引

4

2 回答 2

1

我想你正在寻找这样的东西:

//implementation for AWActionSheet's method: actionForItem:withBlock:

-(void) actionForItem:(UITapGestureRecognizer*)recongizer withBlock:(void(^)(NSInteger integer)) block {

    NSInteger myInt = 0;
    //whatever

    //callback
    block(myInt);
}

和电话

AWActionSheet* actionSheet;
[actionsheet actionForItem:recognizer withBlock:^(NSInteger integer) {
    NSLog(@"myInt: %d", integer);
}];
于 2013-04-11T08:01:08.620 回答
0

使用这个对象:

https://github.com/matteogobbi/MGActionSheet

这是一个按块初始化和使用对象的简单示例:

//Initialization
MGActionSheet *actionSheet = [[MGActionSheet alloc] initWithTitle:@"Block action sheet!" cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:@"Option 1", @"Option 2", @"Option 3", nil];

//Show with completition block
[actionSheet showInView:self.view withChoiceCompletition:^(int buttonIndex) {

    if(buttonIndex == actionSheet.cancelButtonIndex) NSLog(@"Cancelled");
    else if(buttonIndex == actionSheet.destructiveButtonIndex) NSLog(@"Destructed");
    else {
        NSLog(@"Option at index: %d", buttonIndex);
    }
}];
于 2013-11-28T12:34:46.710 回答