简体   繁体   中英

Navigate to another viewController from slide menu? SWIFT

I have slide tableViewMenu, but my navigation from clicked sell to another viewController doesn't work. Here is my code from main view controller:

 @IBAction func menuButton(_ sender: UIBarButtonItem) {
    guard let menuViewController = storyboard?.instantiateViewController(withIdentifier: "menuViewController") as? MenuTableViewController else {
        return
    }
    menuViewController.didTapMenuType = { menuType in
        print(menuType)
        self.transitionToNew(menuType)
    }
    
    menuViewController.modalPresentationStyle = .overCurrentContext
    menuViewController.transitioningDelegate = self
    present(menuViewController, animated: true)
}



func transitionToNew(_ menuType: MenuType){
    
    switch menuType {
    case .favourites:
        let VC = self.storyboard?.instantiateViewController(identifier: "favourites") as! FavouriteStationsTableViewController
        self.present(VC, animated: true, completion: nil)
    case .nightMode:
            let appDelegate = UIApplication.shared.windows.first
                appDelegate?.overrideUserInterfaceStyle = .dark
           
    default:
        break
    }
}

}

and in MenuViewController is this code:

enum MenuType: Int {

case home
case favourites
case nightMode

}

class MenuTableViewController: UITableViewController {

var didTapMenuType: ((MenuType) -> Void)?


override func viewDidLoad() {
    super.viewDidLoad()
    
}

// MARK: - Table view data source


override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    guard let menuType = MenuType(rawValue: indexPath.row) else {
        return
    }
    
    dismiss(animated: true) {
        
        [weak self] in
        print("Dismissing: \(menuType)")
        self?.didTapMenuType?(menuType)
      }
}

When I try to navigate, the Menu View Controller just dismissed.

In this enum:

enum MenuType: Int {

case home
case favourites
case nightMode

}

.home equals 0 (zero)

.favourites equals 1

.nightMode equals 2

You're setting:

`let menuType = MenuType(rawValue: indexPath.row)` 

and you're probably tapping on the first row, which will be 0 (zero). So, you're setting menuType to .home .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM