-1

I am quite new in iOS development and looking for your advice.

I am looking at one project at GitHub.

I would like to add some functionality. I would like to add a button which will show SideMenu and possibly hide it.

The sideMenu is an instance of class SideMenu and it is created in ViewDidLoad method. How to call sideMenu.toggleMenu(true) method using button's action method.

class HomeViewController: UIViewController, SideMenuDelegate {

    @IBAction func menuButtonTapped(_ sender: Any) {

     sideMenu.toggleMenu(open: true)

    }


    override func viewDidLoad() {
        super.viewDidLoad()

    let sideMenu = SideMenu(menuWidth: 200 , menuItemTitles: ["", "", "Profile", "Settings", "Restaurant"], parentViewController: vviewController)

        sideMenu.menuDelegate = self

}

My button is created using storyboard that is why I can't add it is action method into viewDidLoad.

Theoretically I could create button programmatically in ViewDidLoad. Is it the only solution?

enter image description here

Almazini
  • 1,825
  • 4
  • 25
  • 48
  • I don't understand, are you using storyboard for your button or making it programmatically? It seems that you want it to be programmatically, but then why do you have an IBAction for your button? – Benjamin Lowry Jan 07 '17 at 11:27
  • If you are using storyboard. Did you hook up the @IBAction using ctrl-dragging? Make a quick print statement in your menuButtonTapped to see if it is executed when you click it – Emptyless Jan 07 '17 at 11:27
  • Presumably your code is not compiling at the moment because sideMenu is a local constant to viewDidLoad and not accessible from the IBAction? – Ali Beadle Jan 07 '17 at 11:29
  • @AliBeadle exactly. problem is that variable is local for other method and I cant access it. Because I cant create it out of ViewDidLoad as I need to transfer view as a parameter – Almazini Jan 07 '17 at 11:37
  • `sideMenu` seems not to have any _global_ reference; therefore you cannot use the _local_ one outside of the local scope which has it. – holex Jan 07 '17 at 11:41

1 Answers1

1

The problem is nothing that do with your button or where you declare it.

sideMenu in your code is a constant of viewDidLoad and not accessible to the IBAction.

Move your definition of sideMenu outside of the method and make it optional:

var sideMenu:SideMenu?

In viewDidLoad set it as you currently are (remove the let)

sideMenu = SideMenu(...

In the IBAction, unwrap the optional to use it:

if let sideMenu = sideMenu {
    // do what you need with sideMenu
else {
    // handle error sideMenu not initialised properly
}

See vacawama's comments for alternatives.

Rajat
  • 10,977
  • 3
  • 38
  • 55
Ali Beadle
  • 4,486
  • 3
  • 30
  • 55
  • 1
    No need to unwrap. Just use optional chaining when accessing it... `sideMenu?.toggleMenu(open: true)`, `sideMenu?.menuDelegate = self`, etc. – vacawama Jan 07 '17 at 11:42
  • @vacawama good point, although that will hide an error of if sideView does not unit properly. Will edit answer. – Ali Beadle Jan 07 '17 at 11:45
  • 1
    Sure, but that error is a one-time programming error and not something that needs to be dealt with repeatedly. In fact, making it `var sideMenu: SideMenu!` might actually be the best way to go, because then you'll know immediately that you forgot to set it. This is how `@IBOutlets` are dealt with. – vacawama Jan 07 '17 at 11:46