选择菜单项后如何显示viewController?

时间:2019-09-12 10:04:34

标签: uitableview menu side-menu

我在菜单中选择一个项目后试图带出视图控制器。目前,当我按某些按钮时,只需更改页面即可,但页面卡在右侧。我怎么把它拿出来。

这是我编写的代码。

class ContainerVC: UIViewController {

var sideMenuOpen = false
@IBOutlet weak var mainView: UIView!


override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self,
                                           selector: #selector(toggleSideMenu),
                                           name: NSNotification.Name("ToggleSideMenu"),
                                           object: nil)
  }

@IBOutlet weak var sideMenuConstraint: NSLayoutConstraint!

@objc func toggleSideMenu() {
    if sideMenuOpen {
        sideMenuOpen = false
        sideMenuConstraint.constant = -270

    } else {
        sideMenuOpen = true
        sideMenuConstraint.constant = 0
    }
    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
} 

}

这就是我在SideMenuVC中写的内容

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(indexPath.row)
   // NotificationCenter.default.post(name: NSNotification.Name ("ToggleSideMenu"), object: nil)

        switch indexPath.row {
        case 0: NotificationCenter.default.post(name: NSNotification.Name ("ShowProfile"), object: nil)
        case 8:    //signs out of Firebase
        try? Auth.auth().signOut()
        //signs out of Facebook
        FBSDKAccessToken.setCurrent(nil)

        let main = UIStoryboard(name: "Main", bundle: nil)
        let second = main.instantiateViewController(withIdentifier: "loginPage")

        self.present(second, animated: true, completion: nil)
    default: break

        //https://www.youtube.com/watch?v=Hl_Re_KLhcY
    }
}

这就是我在HomeVC中写的

 @IBAction func menuButton() {
    print("Toggle Side Menu")
    NotificationCenter.default.post(name: NSNotification.Name("ToggleSideMenu"), object: nil)
} 

enter image description here

我希望显示选定的页面。

1 个答案:

答案 0 :(得分:0)

只需将NotificationCenter.default.post(name: NSNotification.Name ("ToggleSideMenu"), object: nil)添加到您的didSelectRowAt