Closed Quailcreek closed 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() {
// ...
}
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)
}`
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)
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() {
// ...
}
}
Thank you. I appreciate your thorough feedback.
@iDevelopper How can open left menu with UIButton instead of UIBarButton?
@shashi-kiwi ,
Please open a new issue.
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) {