ODActionViewController 1.1.3

ODActionViewController 1.1.3

测试已测试
Lang语言 Obj-CObjective C
许可协议 MIT
发布最后发布2015年12月

Alexey Nazaroff维护。



  • Alexey Nazaroff

类似于在Maps应用程序中的自定义UIActionSheet的控制器。

Example Image

用法

#import <ODActionViewController.h>

- (void)showActionSheet {
    // Simple single action item
    ODActionControllerItem *singleItem = [ODActionControllerItem itemWithTitle:@"Do something" block:^(id sender){
        NSLog(@"Something was done");
    }];

    // Group of items. It will be shown as single section
    ODActionControllerItem *groupItem = [ODActionControllerItem new];
    groupItem.subitems = @[
        [ODActionControllerItem itemWithTitle:@"Do a" block:^(id sender){
            NSLog(@"A was done");
        }],
        [ODActionControllerItem itemWithTitle:@"Do b" block:^(id sender){
            NSLog(@"B was done");
        }]
    ];

    // Our custom item with segmented control
    MYSegmentedActionItem *segmentedItem = [MYSegmentedActionItem new];
    segmentedItem.customCellClass = MYActionSegmentedCell.class;
    segmentedItem.selectedIndex = 1;
    segmentedItem.block = ^(MYSegmentedActionItem * _Nonnull sender) {
        NSLog(@"Idx: %d", (int)sender.selectedIndex);
    };

    UIViewController *vc = [[ODActionViewController alloc] initWithActionItems:@[segmentedItem, singleItem, groupItem] cancelButtonTitle:@"Cancel"];
    [self od_presentActionViewController:vc animated:NO completion:nil];
}

自定义单元格

可以使用自定义单元格来构建操作菜单。使用customCellClass属性设置单元格的类。可以在上面的示例中看到。让我们创建一个类似于苹果的Mapps应用程序的分段控制器菜单。我们需要添加两个新的类:MYSegmentedActionItemMYActionSegmentedCell

// Action item
@interface MYSegmentedActionItem: ODActionControllerItem
@property (nonatomic, assign) NSUInteger selectedIndex;
@end

// Cell with segmented control
@interface MYActionSegmentedCell : ODActionViewCell
@end

实现

// Insets for segmented control
static CGSize kCellSegmentInsets = (CGSize){14, 8};

// Empty implementation for item
@implementation MYSegmentedActionItem
@end

// And all logic for our segmented cell
@implementation MYActionSegmentedCell {
    UISegmentedControl *_segmentedControl;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        // Create our segmented control
        _segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"Standard", @"Hybrid", @"Satelite"]];
        _segmentedControl.frame = CGRectInset(self.bounds, kCellSegmentInsets.width, kCellSegmentInsets.height);
        _segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [_segmentedControl addTarget:self action:@selector(segment_onChange:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:_segmentedControl];
    }
    return self;
}

// You need override this method to configure cell information. It will be invoked in `cellForRowAtIndexPath...`.
- (void)setItem:(MYSegmentedActionItem *)item {
    [super setItem:item];
    _segmentedControl.selectedSegmentIndex = item.selectedIndex;
}

- (void)segment_onChange:(UISegmentedControl *)sender {
    ((MYSegmentedActionItem *)self.item).selectedIndex = sender.selectedSegmentIndex;

    // Check what item is enabled
    if (!self.item.isDisabled && self.item.block) {
        self.item.block(self.item);
    }

    // You need call dismiss method yourself
    if (self.actionDelegate) {
        [self.actionDelegate dismissController];
    }
}

@end

安装

ODActionViewController 通过CocoaPods可用。要安装它,只需将以下行添加到Podfile中:

pod "ODActionViewController"

作者

Alexey Nazaroff,[email protected]

许可协议

ODActionViewController 捆绑在MIT许可协议下。有关更多信息,请参阅LICENSE文件。