Alua-Kinzhebayeva / iOS-PDF-Reader

PDF Reader for iOS written in Swift
MIT License
533 stars 150 forks source link

Override actionButton Button #24

Closed benbahrenburg closed 7 years ago

benbahrenburg commented 7 years ago

Hello,

Thanks for all of the work on this great library. I'm currently using this on one of my projects an need to override the actionButton button.

In the latest build the logic has been updated alittle on this. Before I sent a PR wanted to check what approach you might this is best.

The easiest is to make the actionButton public. This would allow the setting application to override.

fileprivate var actionButton: UIBarButtonItem?

I noticed you updated the create logic to use a new method. Pretty neat. Below is my take on how the calling application could pass in the actionButton as part of this approach.

What which strategy (if any) do you think falls most inline with your thinking?

extension PDFViewController {
    /// Initializes a new `PDFViewController`
    ///
    /// - parameter document:          PDF document to be displayed
    /// - parameter title:             title that displays on the navigation bar on the PDFViewController; if nil, uses document's filename
    /// - parameter actionButtonImage: image of the action button; if nil, uses the default action system item image
    /// - parameter actionStyle:       sytle of the action button
    ///
    /// - returns: a `PDFViewController`
    public class func createNew(with document: PDFDocument, title: String? = nil, actionButtonImage: UIImage? = nil, actionStyle: ActionStyle = .print) -> PDFViewController {
        let storyboard = UIStoryboard(name: "PDFReader", bundle: Bundle(for: PDFViewController.self))
        let controller = storyboard.instantiateInitialViewController() as! PDFViewController

        if let actionButtonImage = actionButtonImage {
            let actionButton = UIBarButtonItem(image: actionButtonImage, style: .plain, target: controller, action: #selector(actionButtonPressed))
            return PDFViewController.createNew(with: document, title: title, actionButton: actionButton,actionStyle: actionStyle)
        }

        let actionButton = UIBarButtonItem(barButtonSystemItem: .action, target: controller, action: #selector(actionButtonPressed))

        return PDFViewController.createNew(with: document, title: title, actionButton: actionButton,actionStyle: actionStyle)
    }

    /// Initializes a new `PDFViewController`
    ///
    /// - parameter document:          PDF document to be displayed
    /// - parameter title:             title that displays on the navigation bar on the PDFViewController; if nil, uses document's filename
    /// - parameter actionButton:      UIBarButtonItem used to override the default action button
    /// - parameter actionStyle:       sytle of the action button
    ///
    /// - returns: a `PDFViewController`
    public class func createNew(with document: PDFDocument, title: String? = nil, actionButton: UIBarButtonItem, actionStyle: ActionStyle = .print) -> PDFViewController {
        let storyboard = UIStoryboard(name: "PDFReader", bundle: Bundle(for: PDFViewController.self))
        let controller = storyboard.instantiateInitialViewController() as! PDFViewController
        controller.document = document
        controller.actionStyle = actionStyle

        if let title = title {
            controller.title = title
        } else {
            controller.title = document.fileName
        }

        controller.actionButton = actionButton

        return controller
    }
}
ranunez commented 7 years ago

This looks good overall, I like the extra createNew function to solve this potential need. What specific need do you have of overriding actionButton? The current implementation has the ability to customize the image and action, and I just was curious to see how you were using it and gauge how big of a need this is. Thanks!!

ranunez commented 7 years ago

Closing due to lack of response and activity for some time. Feel free to reopen if you have more information about the specific need for this feature.