Skip to content

Commit

Permalink
Use first action for cancel button in action sheet
Browse files Browse the repository at this point in the history
If no actions with style `.preferred` were specified, the action sheet should
use the first action in the array since it still needs a cancel button.
  • Loading branch information
sberrevoets committed Sep 12, 2016
1 parent c0b64e7 commit 62c8f8f
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions Source/Views/ActionSheetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ final class ActionSheetView: AlertControllerView {
@IBOutlet private var titleWidthConstraint: NSLayoutConstraint!

override var actions: [AlertAction] {
didSet {
if let cancelActionIndex = self.actions.index(where: { $0.style == .preferred }) {
self.cancelAction = self.actions[cancelActionIndex]
self.actions.remove(at: cancelActionIndex)
}
}
didSet { self.assignCancelAction() }
}

override var actionTappedHandler: ((AlertAction) -> Void)? {
Expand Down Expand Up @@ -77,6 +72,16 @@ final class ActionSheetView: AlertControllerView {

self.actionTappedHandler?(action)
}

private func assignCancelAction() {
if let cancelActionIndex = self.actions.index(where: { $0.style == .preferred }) {
self.cancelAction = self.actions[cancelActionIndex]
self.actions.remove(at: cancelActionIndex)
} else {
self.cancelAction = self.actions.first
self.actions.removeFirst()
}
}
}

private extension UIImage {
Expand Down

0 comments on commit 62c8f8f

Please sign in to comment.