Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c42e8ae

Browse files
committed
UIAlertView button indexes don't necessarily correspond to their layout ordering in terms of cancel button placement. Let UIAlertView handle it, rather than explicitly adding cancel as an 'other button'
1 parent 772c8b9 commit c42e8ae

1 file changed

Lines changed: 7 additions & 18 deletions

File tree

BlocksKit/UIKit/UIAlertView+BlocksKit.m

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,28 +119,17 @@ + (void)load {
119119

120120
+ (void) showAlertViewWithTitle: (NSString *) title message: (NSString *) message cancelButtonTitle: (NSString *) cancelButtonTitle otherButtonTitles: (NSArray *) otherButtonTitles handler: (void (^)(UIAlertView *, NSInteger)) block
121121
{
122-
UIAlertView *alertView = [[self class] alertViewWithTitle: title message: message];
123-
124-
// Set other buttons
125-
if (otherButtonTitles.count)
126-
{
127-
NSUInteger firstOtherButton = [alertView addButtonWithTitle: otherButtonTitles[0]];
128-
[alertView setValue: @(firstOtherButton) forKey: @"firstOtherButton"];
129-
130-
otherButtonTitles = [otherButtonTitles subarrayWithRange: NSMakeRange(1, otherButtonTitles.count - 1)];
131-
[otherButtonTitles each: ^(NSString *button) {
132-
[alertView addButtonWithTitle: button];
133-
}];
134-
}
135-
136122
// If no buttons were specified, cancel button becomes "Dismiss"
137123
if (!cancelButtonTitle.length && !otherButtonTitles.count)
138124
cancelButtonTitle = NSLocalizedString(@"Dismiss", nil);
139125

140-
// Set cancel button
141-
if (cancelButtonTitle.length)
142-
alertView.cancelButtonIndex = [alertView addButtonWithTitle: cancelButtonTitle];
143-
126+
UIAlertView *alertView = [[[self class] alloc] initWithTitle:title message:message delegate:self.dynamicDelegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];
127+
128+
// Set other buttons
129+
[otherButtonTitles each: ^(NSString *button) {
130+
[alertView addButtonWithTitle: button];
131+
}];
132+
144133
// Set `didDismissBlock`
145134
if (block) alertView.didDismissBlock = block;
146135

0 commit comments

Comments
 (0)