diff --git a/Fleet/CoreExtensions/UIAlertController+Fleet.swift b/Fleet/CoreExtensions/UIAlertController+Fleet.swift index 93791a7..202ccf0 100644 --- a/Fleet/CoreExtensions/UIAlertController+Fleet.swift +++ b/Fleet/CoreExtensions/UIAlertController+Fleet.swift @@ -13,10 +13,17 @@ public extension UIAlertController { } if let actionWithTitle = filteredActions.first { + let isCancelStyle = actionWithTitle.style == .Cancel if let handler = actionWithTitle.handler { handler(actionWithTitle) } else { - Logger.logWarning("Action with title \"\(title)\" has no handler") + if !isCancelStyle { + Logger.logWarning("Action with title \"\(title)\" has no handler and is not Cancel style") + } + } + + if isCancelStyle { + presentingViewController?.dismissViewControllerAnimated(true, completion: nil) } } else { Logger.logWarning("No action with title \"\(title)\" found on alert") diff --git a/FleetTestApp/Storyboards/SpottedTurtleViewController.swift b/FleetTestApp/Storyboards/SpottedTurtleViewController.swift index a55533c..595531a 100644 --- a/FleetTestApp/Storyboards/SpottedTurtleViewController.swift +++ b/FleetTestApp/Storyboards/SpottedTurtleViewController.swift @@ -15,10 +15,7 @@ class SpottedTurtleViewController: UIViewController { alertController.addAction(alertAction) - let dismissAction = UIAlertAction(title: "Cower", style: .Cancel) { action in - self.dismissViewControllerAnimated(true, completion: nil) - } - + let dismissAction = UIAlertAction(title: "Cower", style: .Cancel, handler: nil) alertController.addAction(dismissAction) presentViewController(alertController, animated: true, completion: nil) diff --git a/FleetTests/CoreExtensions/UIAlertController+FleetSpec.swift b/FleetTests/CoreExtensions/UIAlertController+FleetSpec.swift index 500420f..4b34826 100644 --- a/FleetTests/CoreExtensions/UIAlertController+FleetSpec.swift +++ b/FleetTests/CoreExtensions/UIAlertController+FleetSpec.swift @@ -26,4 +26,15 @@ class UIAlertController_FleetSpec: XCTestCase { expect(self.viewControllerThatPresentsAlerts.presentedViewController).to(beNil()) expect(self.viewControllerThatPresentsAlerts.informationalLabel?.text).to(equal("WAIT NO PUT ME DOWN")) } + + func test_tapAlertActionWithTitle_whenActionWithThatTitleExistsOnAlert_whenActionIsCancelStyle_dismissesAlert() { + let alertWithCancelAction = UIAlertController() + alertWithCancelAction.addAction(UIAlertAction(title: "Go Away", style: .Cancel, handler: nil)) + viewControllerThatPresentsAlerts.presentViewController(alertWithCancelAction, animated: false, completion: nil) + + let alertController = Fleet.getCurrentScreen()?.presentedAlert + alertController?.tapAlertActionWithTitle("Go Away") + + expect(self.viewControllerThatPresentsAlerts.presentedViewController).to(beNil()) + } }