iDevelopper / PBRevealViewController

A UIViewController subclass for revealing a left and/or right view controller above or below a main view controller.
MIT License
80 stars 16 forks source link

Is there a way to enable/disable the menu from opening? #69

Closed Quailcreek closed 5 years ago

Quailcreek commented 5 years ago

Hi, I have a question. I want to control whether the menu open based upon the status of another button. What I mean is when the menu button is tapped I want to check the isEnabled status of the save button and then control if the menu opens or not.

Thank you for the help, Tom

Here's the code I have:

` @IBAction func menuButtonTapped(_ sender: UIBarButtonItem) {

    if self.saveButton.isEnabled {
        let theSaveAlert = UIAlertController(title: "Do you want to save the changes?",
                                             message: "If not your changes will be lost.",
                                             preferredStyle: .alert)

        theSaveAlert.addAction(UIAlertAction(title: "Cancel",
                                             style: .cancel,
                                             handler: { (action) in

            // Here I want to open the menu the next time the menu button is tapped
            self.saveButton.isEnabled = false
            sender.target = self.revealViewController()
            sender.action = #selector(PBRevealViewController.revealLeftView)
        }))

        theSaveAlert.addAction(UIAlertAction(title: "Save",
                                             style: .default,
                                             handler: { (action) in

            self.saveTheFigure()
            self.saveButton.isEnabled = false
            // Here I want to open the menu the next time the menu button is tapped
        }))

        present(theSaveAlert, animated: true)

    } else { // The save button is disabled

        // Here I want to open the menu when when the menu button is first tapped
        sender.target = self.revealViewController()
        sender.action = #selector(PBRevealViewController.revealLeftView)
    }
}`
iDevelopper commented 5 years ago

Hi,

Use the delegate for that:

    // MARK: - PBRevealViewController delegate

    func revealController(_ revealController: PBRevealViewController, shouldShowLeft viewController: UIViewController) -> Bool
    {
        if self.saveButton.isEnabled {
            self.saveAlert()
            return false

        }
        return true
    }

    func revealControllerPanGestureShouldBegin(_ revealController: PBRevealViewController, direction: PBRevealControllerPanDirection) -> Bool
    {
        if direction == .right && self.saveButton.isEnabled {
            self.saveAlert()
            return false

        }
        return true
    }

    func saveAlert() {
        let theSaveAlert = UIAlertController(title: "Do you want to save the changes?",
                                             message: "If not your changes will be lost.",
                                             preferredStyle: .alert)

        theSaveAlert.addAction(UIAlertAction(title: "Cancel",
                                             style: .cancel,
                                             handler: { (action) in

                                                self.saveButton.isEnabled = false
                                                self.revealViewController()?.revealLeftView()
        }))

        theSaveAlert.addAction(UIAlertAction(title: "Save",
                                             style: .default,
                                             handler: { (action) in

                                                self.saveTheFigure()
                                                self.saveButton.isEnabled = false
        }))

        present(theSaveAlert, animated: true)
    }

    func saveTheFigure() {
        // ...
    }
Quailcreek commented 5 years ago

Hi, Thank you for your reply. I added the functions like you suggested. I guess I still need a little hand holding. How do I trigger the functions? I tried the code below and a couple of other things but shouldShowLeft function never gets fired. Sorry to be a pest but I've only been using Xcode/Swift for about 3 months.

` @IBAction func menuButtonTapped(_ sender: UIBarButtonItem) {

        sender.target = self.revealViewController()
        sender.action = #selector(PBRevealViewController.revealLeftView)

}`
Quailcreek commented 5 years ago

I think I have it. I added this to viewDidLoad and it seems to be working.

menuButton.target = self.revealViewController() menuButton.action = #selector(PBRevealViewController.revealLeftView)

iDevelopper commented 5 years ago

Yes you have to set the target and action for your menuButton in viewDidLoad.

Your controller must adopt PBRevealViewControllerDelegate and you have to set the delegate.

Example:

import UIKit

class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, PBRevealViewControllerDelegate {

    @IBOutlet weak var menuButton: UIBarButtonItem!

    @IBOutlet weak var saveButton: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.revealViewController()?.delegate = self

        self.menuButton.target = self.revealViewController()
        self.menuButton.action = #selector(PBRevealViewController.revealLeftView)

        self.saveButton.isEnabled = false
    }

    @IBAction func saveAction(_ sender: Any) {
        self.saveTheFigure()
        self.saveButton.isEnabled = false
    }

    // MARK: - Table view data source

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 25
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel?.text = "Item \(indexPath.row)"

        return cell
    }

    // MARK: - Table view delegate

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        self.saveButton.isEnabled = true
    }

    // MARK: - PBRevealViewController delegate

    func revealController(_ revealController: PBRevealViewController, shouldShowLeft viewController: UIViewController) -> Bool
    {
        if self.saveButton.isEnabled {
            self.saveAlert()
            return false

        }
        return true
    }

    func revealControllerPanGestureShouldBegin(_ revealController: PBRevealViewController, direction: PBRevealControllerPanDirection) -> Bool
    {
        if direction == .right && self.saveButton.isEnabled {
            self.saveAlert()
            return false

        }
        return true
    }

    func saveAlert() {
        let theSaveAlert = UIAlertController(title: "Do you want to save the changes?",
                                             message: "If not your changes will be lost.",
                                             preferredStyle: .alert)

        theSaveAlert.addAction(UIAlertAction(title: "Cancel",
                                             style: .cancel,
                                             handler: { (action) in

                                                self.saveButton.isEnabled = false
                                                self.revealViewController()?.revealLeftView()
        }))

        theSaveAlert.addAction(UIAlertAction(title: "Save",
                                             style: .default,
                                             handler: { (action) in

                                                self.saveTheFigure()
                                                self.saveButton.isEnabled = false
        }))

        present(theSaveAlert, animated: true)
    }

    func saveTheFigure() {
        // ...
    }
}
Quailcreek commented 5 years ago

Thank you. I appreciate your thorough feedback.

shashi-kiwi commented 4 years ago

@iDevelopper How can open left menu with UIButton instead of UIBarButton?

iDevelopper commented 4 years ago

@shashi-kiwi ,

Please open a new issue.