0

I am new to iOS development and I am making a left menu bar like many applications. I got to a point but now I am stuck. Please help.

I have a class called LeftMenu and it is declared as follows:

class LeftMenu: UIView, UITableViewDelegate, UITableViewDataSource {

The reason is I am using the same LeftMenu in multiple UIViewControllers

There is a tableView which represents the scrollMenu and I am observing the selected row like this:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    switch self.menuItems[indexPath.row] {
    case "Wever":
        print(self.menuItems[indexPath.row])
    case "Payments":
        print(self.menuItems[indexPath.row])
    case "Profile":
        print(self.menuItems[indexPath.row])
    case "Trip History":
        print(self.menuItems[indexPath.row])
    case "Referral":
        print(self.menuItems[indexPath.row])
    case "Help":
        print(self.menuItems[indexPath.row])
    case "Settings":
        print(self.menuItems[indexPath.row])
    case "About":
        print(self.menuItems[indexPath.row])
    default:
        break
    }
}

I use the LeftMenu for bringing up menu in the each of the view controller.

What I would like to do is open the corresponding view controller when a cell in the tableView is selected. Please help.

Prato Das
  • 41
  • 7
  • where are your corresponding view controllers? In storyboard?? have you defined the `segue` in storyboard?? – Bilal Nov 08 '17 at 10:04
  • Unrelated note to the question; you can optimise that `didSelectRowAt` method by assigning your menuItems object to a string and performing your switch over it! Good luck with the future :) – royalmurder Nov 08 '17 at 10:04
  • @Bilal I have not used storyboards. I am making the interface programmatically. – Prato Das Nov 08 '17 at 10:06
  • Hello and welcome to SO. Your question has been asked many times before. E.g. https://stackoverflow.com/questions/39450124/swift-programmatically-navigate-to-another-view-controller-scene And you seem to be confused about what your real problem is. Which is "how do I open a view controller programmatically". Please, focus on your main problem, because all the details about the side view and the way your app works are not relevant to your main question. – FreeNickname Nov 08 '17 at 10:12
  • @FreeNickname Thanks for the link. I am not sure how to use self.pushViewController in a subclass of UIView. – Prato Das Nov 08 '17 at 10:16
  • @PratoDas, sorry, fair enough. I retracted the close vote. This may be helpful to you then: https://stackoverflow.com/questions/1340434/get-to-uiviewcontroller-from-uiview – FreeNickname Nov 08 '17 at 10:20
  • @PratoDas you need to implement protocols to get it work. – Tushar Sharma Nov 08 '17 at 10:24
  • @TusharSharma can I get some hint on how to do that? – Prato Das Nov 08 '17 at 10:25
  • @PratoDas you can google it, you can get may tutorials. – Tushar Sharma Nov 08 '17 at 10:30
  • Possible duplicate of [How to Navigate UIViewController from UIView](https://stackoverflow.com/questions/41849425/how-to-navigate-uiviewcontroller-from-uiview) – karthikeyan Nov 08 '17 at 10:52

1 Answers1

2

You could create a delegate protocol for the LeftMenu view, and have any view controllers displaying the LeftMenu implement that protocol, so they take responsibility for pushing the new view controller.

in LeftMenu.swift

protocol LeftMenuDelegate: class {
    func leftMenuDidSelectViewController(_ viewController: UIViewController)
}

class LeftMenu: UIView, UITableViewDelegate, UITableViewDataSource {
    public weak var delegate: LeftMenuDelegate?
    ...

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch self.menuItems[indexPath.row] {
        case "Wever":
            let viewController = WeverViewController() // or whatever it is named
            self.delegate?.leftMenuDidSelectViewController(viewController)
            ...
        }
    }
    ...
}

Then in all view controllers that display LeftMenu:

class SomeViewController: UIViewController, LeftMenuDelegate {
    ...
    // wherever you create you left menu, do this
    let leftMenu = LeftMenu()
    leftMenu.delegate = self
    ...

    // Add this method to be called from LeftMenu through the delegate protocol
    func leftMenuDidSelectViewController(_ viewController: UIViewController) {
        self.navigationController?.pushViewController(viewController, animated: true)
    }

You could create a base class that does this for all the view controllers that include the LeftMenu view, so you don't have to have this implementation more than once.

Sune
  • 1,326
  • 1
  • 11
  • 17
  • What is the fix? – Prato Das Nov 08 '17 at 11:02
  • I have edited the answer to make it more clear what you should do in the view controllers that create the LeftMenu. Let me know if there is some of it you do not understand – Sune Nov 08 '17 at 11:27
  • I have implemented this, but when I press a cell, the view controller does not get pushed in the case statements. But I have a print statement in the case block, which does print out to the console. – Prato Das Nov 08 '17 at 12:09
  • It worked but I did not want to have the top navigation bar – Prato Das Nov 08 '17 at 12:11
  • You don't have to push a new view controller. You can also just use this method to do whatever changes you need to do to the view in you view controller. So instead of having the LeftMenu send a new view controller, you can have it call a method like setupWeverView() or whatever you need. – Sune Nov 08 '17 at 13:58