Skip to content

Commit 8523a75

Browse files
committed
Merge pull request #79 from ParsePlatform/nlutsenko.alertViewButtons
Fixed PFAlertView cancel button handling logic.
2 parents 98825b8 + 6fb5a28 commit 8523a75

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

Parse/Internal/PFAlertView.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ + (void)showAlertWithTitle:(NSString *)title
3535

3636
void (^alertActionHandler)(UIAlertAction *) = [^(UIAlertAction *action){
3737
// This block intentionally retains alertController, and releases it afterwards.
38-
NSUInteger index = [alertController.actions indexOfObject:action];
39-
completion(index - 1);
38+
if (action.style == UIAlertActionStyleCancel) {
39+
completion(NSNotFound);
40+
} else {
41+
NSUInteger index = [alertController.actions indexOfObject:action];
42+
completion(index - 1);
43+
}
4044
alertController = nil;
4145
} copy];
4246

@@ -85,7 +89,11 @@ + (void)showAlertWithTitle:(NSString *)title
8589

8690
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
8791
if (self.completion) {
88-
self.completion(buttonIndex - alertView.firstOtherButtonIndex);
92+
if (buttonIndex == alertView.cancelButtonIndex) {
93+
self.completion(NSNotFound);
94+
} else {
95+
self.completion(buttonIndex - 1);
96+
}
8997
}
9098
}
9199

Tests/Unit/AlertViewTests.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ - (void)testShowAlertWithAlertViewController {
102102
cancelButtonTitle:@"Cancel"
103103
otherButtonTitles:@[ @"Yes", @"No" ]
104104
completion:^(NSUInteger selectedOtherButtonIndex) {
105-
XCTAssertEqual(selectedOtherButtonIndex, -1);
105+
XCTAssertEqual(selectedOtherButtonIndex, NSNotFound);
106106

107107
[expectation fulfill];
108108
}];
@@ -147,15 +147,15 @@ - (void)testShowWithoutAlertViewController {
147147
[delegate alertView:self clickedButtonAtIndex:0];
148148
});
149149

150-
OCMStub([mockedAlertView firstOtherButtonIndex]).andReturn(1);
150+
OCMStub([mockedAlertView cancelButtonIndex]).andReturn(0);
151151

152152
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
153153
[PFAlertView showAlertWithTitle:@"Title"
154154
message:@"Message"
155155
cancelButtonTitle:@"Cancel"
156156
otherButtonTitles:@[ @"Yes", @"No" ]
157157
completion:^(NSUInteger selectedOtherButtonIndex) {
158-
XCTAssertEqual(selectedOtherButtonIndex, -1);
158+
XCTAssertEqual(selectedOtherButtonIndex, NSNotFound);
159159

160160
[expectation fulfill];
161161
}];

0 commit comments

Comments
 (0)