Skip to content

Commit

Permalink
Added support to display a checkmark with isSelected (#16)
Browse files Browse the repository at this point in the history
* Added support to display a checkmark with `isSelected`

* Added test coverage to `isSelected`

* Renamed `isSelected` to `isChecked`

* Updated README.md

* Updated documentation
  • Loading branch information
pedrommcarrasco authored and BasThomas committed Jan 19, 2020
1 parent 193cf42 commit 4843b30
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Next

## 3.2.0
- Added support to display a checkmark with `isChecked`

## 3.1.0

- Updated to Swift 5.1
Expand Down
3 changes: 3 additions & 0 deletions Example/ImageAlertAction/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class ViewController: UIViewController {

let settings = UIAlertAction(title: "Settings", image: #imageLiteral(resourceName: "settings"), style: .default)
alertController.addAction(settings)

let selectedSettings = UIAlertAction(title: "Checked Settings", image: #imageLiteral(resourceName: "settings"), isChecked: true, style: .default)
alertController.addAction(selectedSettings)

let cancel = UIAlertAction(title: "Cancel", style: .cancel)
alertController.addAction(cancel)
Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- ImageAlertAction (3.1.0)
- ImageAlertAction (3.2.0)

DEPENDENCIES:
- ImageAlertAction (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
ImageAlertAction: 2f4d6ecd9d4896eb4bb959f9d1c8a1101d6805f4
ImageAlertAction: 5556758a632c531172c5b2afcc4a25417df9f5a2

PODFILE CHECKSUM: 2861c5c31891940484aaf82fd18a1725282b6061

Expand Down
4 changes: 2 additions & 2 deletions Example/Pods/Local Podspecs/ImageAlertAction.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Example/Tests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,14 @@ class Tests: XCTestCase {
let action = UIAlertAction(title: "Hello world!", image: #imageLiteral(resourceName: "settings"), style: .cancel)
XCTAssertNotNil(action.image)
}

func testCheckmarkIsCorrectlySet() {
let action = UIAlertAction(title: "Hello world!", image: #imageLiteral(resourceName: "settings"), isChecked: true, style: .cancel)
XCTAssertTrue(action.isChecked)
}

func testCheckmarkIsNotSet() {
let action = UIAlertAction(title: "Hello world!", image: #imageLiteral(resourceName: "settings"), style: .cancel)
XCTAssertFalse(action.isChecked)
}
}
2 changes: 1 addition & 1 deletion ImageAlertAction.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'ImageAlertAction'
s.version = '3.1.0'
s.version = '3.2.0'
s.summary = 'Image support for UIAlertAction'
s.description = <<-DESC
ImageAlertAction adds image support to UIAlertAction.
Expand Down
6 changes: 6 additions & 0 deletions ImageAlertAction/Classes/UIAlertAction+Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import UIKit
extension UIAlertAction {

private var imageKey: String { "image" }
private var isCheckedKey: String { "checked" }

/// Create and return an action with the specified title and behavior.
///
Expand All @@ -24,6 +25,7 @@ extension UIAlertAction {
/// may be used with [UIAlertAction.Style.cancel](https://developer.apple.com/documentation/uikit/uialertaction/style/cancel).
/// - parameter image: An image to display on the left side of the button.
/// Use this to visually convey the action's purpose.
/// - parameter isChecked: A boolean that will be used to determine if a check mark should be displayed on the right side of the title
/// - parameter style: Additional styling information to apply to the button.
/// Use the style information to convey the type of action that is performed by the button.
/// For a list of possible values, see the constants in
Expand All @@ -36,13 +38,17 @@ extension UIAlertAction {
public convenience init(
title: String? = nil,
image: UIImage,
isChecked: Bool = false,
style: UIAlertAction.Style,
handler: ((UIAlertAction) -> Void)? = nil
) {
self.init(title: title, style: style, handler: handler)
setValue(image, forKey: imageKey)
setValue(isChecked, forKey: isCheckedKey)
}

/// The image of the action's button.
public var image: UIImage? { value(forKey: imageKey) as? UIImage }

public var isChecked: Bool { value(forKey: isCheckedKey) as? Bool ?? false }
}
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ let settings = UIAlertAction(
settings.image // returns an optional UIImage
```

#### Adding a checkmark

You can also show a check mark on actions via `isChecked`

```swift
let settings = UIAlertAction(
title: "Settings",
image: #imageLiteral(resourceName: "settings"),
isChecked: true
style: .default
)
settings.isChecked // returns a Bool
```

### Presenting the `UIAlertController`

To present a `UIAlertController` containing the `UIAlertAction`, nothing changes.
Expand Down

0 comments on commit 4843b30

Please sign in to comment.