From bafcd16208499f6036ec12d702c3897f4726dfd5 Mon Sep 17 00:00:00 2001 From: Leo Date: Wed, 26 Apr 2017 11:51:19 +0800 Subject: [PATCH] Append buttons at indexes #34 --- Demo/LCActionSheetDemo/ViewController.m | 25 ++++++++++---- Sources/LCActionSheet.h | 34 ++++++++++++++----- Sources/LCActionSheet.m | 45 +++++++++++++++++++++---- Sources/LCActionSheetConfig.h | 2 +- Sources/LCActionSheetConfig.m | 2 +- 5 files changed, 84 insertions(+), 24 deletions(-) diff --git a/Demo/LCActionSheetDemo/ViewController.m b/Demo/LCActionSheetDemo/ViewController.m index a033734..d98254f 100644 --- a/Demo/LCActionSheetDemo/ViewController.m +++ b/Demo/LCActionSheetDemo/ViewController.m @@ -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]; @@ -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); @@ -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); } @@ -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); } diff --git a/Sources/LCActionSheet.h b/Sources/LCActionSheet.h index a708e8b..a0c42ed 100644 --- a/Sources/LCActionSheet.h +++ b/Sources/LCActionSheet.h @@ -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. @@ -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 @@ -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. @@ -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 @@ -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. @@ -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 *)titles atIndexes:(NSIndexSet *)indexes; /** Show the instance of LCActionSheet. diff --git a/Sources/LCActionSheet.m b/Sources/LCActionSheet.m index f382d0a..61baf6d 100644 --- a/Sources/LCActionSheet.m +++ b/Sources/LCActionSheet.m @@ -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]; } @@ -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 *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 *)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 *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) { @@ -515,7 +548,7 @@ - (void)setButtonHeight:(CGFloat)aButtonHeight { [self updateCancelButton]; } -- (NSInteger)cancelButtonIndex { +- (NSUInteger)cancelButtonIndex { return 0; } @@ -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]; } diff --git a/Sources/LCActionSheetConfig.h b/Sources/LCActionSheetConfig.h index fbd6b58..46dd1eb 100644 --- a/Sources/LCActionSheetConfig.h +++ b/Sources/LCActionSheetConfig.h @@ -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. diff --git a/Sources/LCActionSheetConfig.m b/Sources/LCActionSheetConfig.m index ccb7bed..e2a6fc2 100644 --- a/Sources/LCActionSheetConfig.m +++ b/Sources/LCActionSheetConfig.m @@ -71,7 +71,7 @@ - (instancetype)init { return self; } -- (NSInteger)cancelButtonIndex { +- (NSUInteger)cancelButtonIndex { return 0; }