Skip to content

Add option for auto dismissing in actions #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Library/PMAlertAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ import UIKit
fileprivate var action: (() -> Void)?

open var actionStyle : PMAlertActionStyle
open var autoDismiss : Bool

open var separator = UIImageView()

init(){
self.actionStyle = .cancel
self.autoDismiss = true
super.init(frame: CGRect.zero)
}

@objc public convenience init(title: String?, style: PMAlertActionStyle, action: (() -> Void)? = nil){
@objc public convenience init(title: String?, style: PMAlertActionStyle, autoDismiss: Bool = true, action: (() -> Void)? = nil){
self.init()

self.action = action
Expand All @@ -39,6 +41,7 @@ import UIKit
self.actionStyle = style
style == .default ? (self.setTitleColor(UIColor(red: 191.0/255.0, green: 51.0/255.0, blue: 98.0/255.0, alpha: 1.0), for: UIControl.State())) : (self.setTitleColor(UIColor.gray, for: UIControl.State()))

self.autoDismiss = autoDismiss
self.addSeparator()
}

Expand Down
9 changes: 8 additions & 1 deletion Library/PMAlertController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,14 @@ import UIKit
alertActionStackView.axis = .horizontal
}

alertAction.addTarget(self, action: #selector(PMAlertController.dismissAlertController(_:)), for: .touchUpInside)
if alertAction.autoDismiss {
alertAction.addTarget(self, action: #selector(PMAlertController.dismissAlertController(_:)), for: .touchUpInside)
}
}

@objc open func dismiss(animated: Bool, style: PMAlertActionStyle) {
self.animateDismissWithGravity(style)
self.dismiss(animated: animated, completion: nil)
}

@objc fileprivate func dismissAlertController(_ sender: PMAlertAction){
Expand Down
6 changes: 4 additions & 2 deletions PMAlertControllerSample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ class ViewController: UIViewController {
print("Cancel")
}))

alertVC.addAction(PMAlertAction(title: "Allow", style: .default, action: { () in
let allowAction = PMAlertAction(title: "Allow", style: .default, autoDismiss: false, action: { () in
print("Allow")
}))
alertVC.dismiss(animated: true, style: .default)
})
alertVC.addAction(allowAction)

self.present(alertVC, animated: true, completion: nil)
}
Expand Down