Skip to content

Commit

Permalink
Append buttons at indexes #34
Browse files Browse the repository at this point in the history
  • Loading branch information
iTofu committed Apr 26, 2017
1 parent 71458a3 commit bafcd16
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 24 deletions.
25 changes: 18 additions & 7 deletions Demo/LCActionSheetDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ - (IBAction)showCustomActionSheet {
otherButtonTitles:@"Button 1", @"Button 2", @"Button 3", @"This is a very very very very very very long button title~", @"Button 5", nil];
actionSheet.title = @"This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very long title~";
actionSheet.cancelButtonTitle = @"Close";
[actionSheet appendButtonTitles:@"Button 6", @"Button 7", nil];
[actionSheet appendButtonsWithTitles:@"Button 6", @"Button 7", nil];
actionSheet.titleColor = [UIColor orangeColor];
actionSheet.buttonColor = [UIColor greenColor];
actionSheet.titleFont = [UIFont boldSystemFontOfSize:15.0f];
Expand All @@ -60,7 +60,7 @@ - (IBAction)showCustomActionSheet {
}

- (IBAction)showBlockActionSheet {
LCActionSheet *actionSheet = [LCActionSheet sheetWithTitle:@"Block LCActionSheet" cancelButtonTitle:@"Cancel" clicked:^(LCActionSheet *actionSheet, NSInteger buttonIndex) {
LCActionSheet *actionSheet = [LCActionSheet sheetWithTitle:@"Block LCActionSheet" cancelButtonTitle:@"Cancel" clicked:^(LCActionSheet *actionSheet, NSUInteger buttonIndex) {

NSLog(@"clickedButtonAtIndex: %d", (int)buttonIndex);

Expand All @@ -79,20 +79,31 @@ - (IBAction)showBlockActionSheet {
NSLog(@"didPresentActionSheet");
};

actionSheet.willDismissHandler = ^(LCActionSheet *actionSheet, NSInteger buttonIndex) {
actionSheet.willDismissHandler = ^(LCActionSheet *actionSheet, NSUInteger buttonIndex) {
NSLog(@"willDismissWithButtonIndex: %d", (int)buttonIndex);
};

actionSheet.didDismissHandler = ^(LCActionSheet *actionSheet, NSInteger buttonIndex) {
actionSheet.didDismissHandler = ^(LCActionSheet *actionSheet, NSUInteger buttonIndex) {
NSLog(@"didDismissWithButtonIndex: %d", (int)buttonIndex);
};

[actionSheet show];


// Append buttons methods
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
// [actionSheet appendButtonWithTitle:@"WoW" atIndex:1];

NSMutableIndexSet *set = [[NSMutableIndexSet alloc] init];
[set addIndex:7];
[set addIndex:8];
[actionSheet appendButtonsWithTitles:@[@"Hello", @"World"] atIndexes:set];
});
}

#pragma mark - LCActionSheet Delegate

- (void)actionSheet:(LCActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
- (void)actionSheet:(LCActionSheet *)actionSheet clickedButtonAtIndex:(NSUInteger)buttonIndex {
NSLog(@"clickedButtonAtIndex: %d", (int)buttonIndex);
}

Expand All @@ -104,11 +115,11 @@ - (void)didPresentActionSheet:(LCActionSheet *)actionSheet {
NSLog(@"didPresentActionSheet");
}

- (void)actionSheet:(LCActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
- (void)actionSheet:(LCActionSheet *)actionSheet willDismissWithButtonIndex:(NSUInteger)buttonIndex {
NSLog(@"willDismissWithButtonIndex: %d", (int)buttonIndex);
}

- (void)actionSheet:(LCActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
- (void)actionSheet:(LCActionSheet *)actionSheet didDismissWithButtonIndex:(NSUInteger)buttonIndex {
NSLog(@"didDismissWithButtonIndex: %d", (int)buttonIndex);
}

Expand Down
34 changes: 25 additions & 9 deletions Sources/LCActionSheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ NS_ASSUME_NONNULL_BEGIN
/**
Handle click button.
*/
typedef void(^LCActionSheetClickedHandler)(LCActionSheet *actionSheet, NSInteger buttonIndex);
typedef void(^LCActionSheetClickedHandler)(LCActionSheet *actionSheet, NSUInteger buttonIndex);

/**
Handle action sheet will present.
Expand All @@ -53,11 +53,11 @@ typedef void(^LCActionSheetDidPresentHandler)(LCActionSheet *actionSheet);
/**
Handle action sheet will dismiss.
*/
typedef void(^LCActionSheetWillDismissHandler)(LCActionSheet *actionSheet, NSInteger buttonIndex);
typedef void(^LCActionSheetWillDismissHandler)(LCActionSheet *actionSheet, NSUInteger buttonIndex);
/**
Handle action sheet did dismiss.
*/
typedef void(^LCActionSheetDidDismissHandler)(LCActionSheet *actionSheet, NSInteger buttonIndex);
typedef void(^LCActionSheetDidDismissHandler)(LCActionSheet *actionSheet, NSUInteger buttonIndex);


#pragma mark - LCActionSheet Delegate
Expand All @@ -69,7 +69,7 @@ typedef void(^LCActionSheetDidDismissHandler)(LCActionSheet *actionSheet, NSInte
/**
Handle click button.
*/
- (void)actionSheet:(LCActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
- (void)actionSheet:(LCActionSheet *)actionSheet clickedButtonAtIndex:(NSUInteger)buttonIndex;

/**
Handle action sheet will present.
Expand All @@ -83,11 +83,11 @@ typedef void(^LCActionSheetDidDismissHandler)(LCActionSheet *actionSheet, NSInte
/**
Handle action sheet will dismiss.
*/
- (void)actionSheet:(LCActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex;
- (void)actionSheet:(LCActionSheet *)actionSheet willDismissWithButtonIndex:(NSUInteger)buttonIndex;
/**
Handle action sheet did dismiss.
*/
- (void)actionSheet:(LCActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;
- (void)actionSheet:(LCActionSheet *)actionSheet didDismissWithButtonIndex:(NSUInteger)buttonIndex;

@end

Expand All @@ -112,7 +112,7 @@ typedef void(^LCActionSheetDidDismissHandler)(LCActionSheet *actionSheet, NSInte
/**
Cancel button's index.
*/
@property (nonatomic, assign, readonly) NSInteger cancelButtonIndex;
@property (nonatomic, assign, readonly) NSUInteger cancelButtonIndex;

/**
LCActionSheet's delegate.
Expand Down Expand Up @@ -356,9 +356,25 @@ typedef void(^LCActionSheetDidDismissHandler)(LCActionSheet *actionSheet, NSInte
/**
Append buttons with titles.
@param buttonTitles buttonTitles
@param titles titles
*/
- (void)appendButtonTitles:(nullable NSString *)buttonTitles, ... NS_REQUIRES_NIL_TERMINATION;
- (void)appendButtonsWithTitles:(nullable NSString *)titles, ... NS_REQUIRES_NIL_TERMINATION;

/**
Append button at index with title.
@param title title
@param index index
*/
- (void)appendButtonWithTitle:(nullable NSString *)title atIndex:(NSUInteger)index;

/**
Append buttons at indexs with titles.
@param titles titles
@param indexes indexes
*/
- (void)appendButtonsWithTitles:(NSArray<NSString *> *)titles atIndexes:(NSIndexSet *)indexes;

/**
Show the instance of LCActionSheet.
Expand Down
45 changes: 39 additions & 6 deletions Sources/LCActionSheet.m
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,13 @@ - (void)setupView {
self.cancelButton = cancelButton;
}

- (void)appendButtonTitles:(NSString *)buttonTitles, ... {
- (void)appendButtonsWithTitles:(NSString *)titles, ... {
id eachObject;
va_list argumentList;
NSMutableArray *tempButtonTitles = nil;
if (buttonTitles) {
tempButtonTitles = [[NSMutableArray alloc] initWithObjects:buttonTitles, nil];
va_start(argumentList, buttonTitles);
if (titles) {
tempButtonTitles = [[NSMutableArray alloc] initWithObjects:titles, nil];
va_start(argumentList, titles);
while ((eachObject = va_arg(argumentList, id))) {
[tempButtonTitles addObject:eachObject];
}
Expand All @@ -372,6 +372,39 @@ - (void)appendButtonTitles:(NSString *)buttonTitles, ... {
[self updateTableView];
}

- (void)appendButtonWithTitle:(NSString *)title atIndex:(NSUInteger)index {
NSAssert(index != 0, @"Index 0 is cancel button");
NSAssert(index <= self.otherButtonTitles.count + 1, @"Index crossed");

NSMutableArray<NSString *> *arrayM = [NSMutableArray arrayWithArray:self.otherButtonTitles];
[arrayM insertObject:title atIndex:index - 1];
self.otherButtonTitles = [NSArray arrayWithArray:arrayM];

[self.tableView reloadData];
[self updateBottomView];
[self updateTableView];
}

- (void)appendButtonsWithTitles:(NSArray<NSString *> *)titles atIndexes:(NSIndexSet *)indexes {
NSAssert(titles.count == indexes.count, @"Count of titles differs from count of indexs");

NSMutableIndexSet *indexSetM = [[NSMutableIndexSet alloc] init];
[indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
NSAssert(idx != 0, @"Index 0 is cancel button");
NSAssert(idx <= self.otherButtonTitles.count + indexes.count, @"Index crossed");

[indexSetM addIndex:idx - 1];
}];

NSMutableArray<NSString *> *arrayM = [NSMutableArray arrayWithArray:self.otherButtonTitles];
[arrayM insertObjects:titles atIndexes:indexSetM];
self.otherButtonTitles = [NSArray arrayWithArray:arrayM];

[self.tableView reloadData];
[self updateBottomView];
[self updateTableView];
}

- (void)handleDidChangeStatusBarOrientation {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeRight || orientation ==UIInterfaceOrientationLandscapeLeft) {
Expand Down Expand Up @@ -515,7 +548,7 @@ - (void)setButtonHeight:(CGFloat)aButtonHeight {
[self updateCancelButton];
}

- (NSInteger)cancelButtonIndex {
- (NSUInteger)cancelButtonIndex {
return 0;
}

Expand Down Expand Up @@ -654,7 +687,7 @@ - (void)show {
}];
}

- (void)hideWithButtonIndex:(NSInteger)buttonIndex {
- (void)hideWithButtonIndex:(NSUInteger)buttonIndex {
if ([self.delegate respondsToSelector:@selector(actionSheet:willDismissWithButtonIndex:)]) {
[self.delegate actionSheet:self willDismissWithButtonIndex:buttonIndex];
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/LCActionSheetConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ NS_ASSUME_NONNULL_BEGIN
/**
Cancel button's index.
*/
@property (nonatomic, assign, readonly) NSInteger cancelButtonIndex;
@property (nonatomic, assign, readonly) NSUInteger cancelButtonIndex;

/**
All destructive buttons' set. You should give it the `NSNumber` type items.
Expand Down
2 changes: 1 addition & 1 deletion Sources/LCActionSheetConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ - (instancetype)init {
return self;
}

- (NSInteger)cancelButtonIndex {
- (NSUInteger)cancelButtonIndex {
return 0;
}

Expand Down

0 comments on commit bafcd16

Please sign in to comment.