简体   繁体   English

如何使用SlideMenuViewcontroller从Mainviewcontroller导航到另一个视图控制器

[英]How to navigate from Mainviewcontroller to another view controller using SlideMenuViewcontroller

I'm trying to navigate from MainViewController to other ViewController when clicked on the rightHeaderButtonClicked method. 单击rightHeaderButtonClicked方法时,我试图从MainViewController导航到其他ViewController。 But, it doesn't navigate. 但是,它无法导航。 I have shared the code below. 我分享了下面的代码。

Appdelegate.swift Appdelegate.swift

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    fileprivate func createMenuView() {
        // create viewController code...
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let mainViewController = storyboard.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
        let menuViewController = storyboard.instantiateViewController(withIdentifier: "MenuViewController") as! MenuViewController
        let nvc: UINavigationController = UINavigationController(rootViewController: mainViewController)
        menuViewController.mainViewController = nvc

        let slideMenuController = ExSlideMenuController(mainViewController:mainViewController, leftMenuViewController: menuViewController)
        slideMenuController.automaticallyAdjustsScrollViewInsets = true
        slideMenuController.delegate = mainViewController
        slideMenuController.removeRightGestures()

        self.window?.backgroundColor = UIColor(red: 236.0, green: 238.0, blue: 241.0, alpha: 1.0)
        self.window?.rootViewController = slideMenuController
        self.window?.makeKeyAndVisible()
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        createMenuView()
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
}

MainViewController.swift MainViewController.swift

class MainViewController: UIViewController,SlideMenuControllerDelegate {

    @IBOutlet weak var chitsTableView: UITableView!

    var mainVc: UIViewController!

    override func viewDidLoad() {
        super.viewDidLoad()

        chitsTableView.register(UINib(nibName: "ChitTableViewCell", bundle: nil), forCellReuseIdentifier: "ChitTableViewCell")
    }

    override func viewWillAppear(_ animated: Bool) {
        self.slideMenuController()?.changeMainViewController(self.allbiddersViewController, close: true)
        self.navigationController?.isNavigationBarHidden = true
    }

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

    override func viewDidAppear(_ animated: Bool) {
        self.slideMenuController()?.addLeftGestures()
    }

    @IBAction func leftHeaderButtonClicked(_ sender: UIBarButtonItem) {
        self.slideMenuController()?.openLeft()
    }

    @IBAction func rightHeaderButtonClicked(_ sender: UIBarButtonItem) {
        let chitdetailVc = self.storyboard?.instantiateViewController(withIdentifier: "ChitDetailViewController") as! ChitDetailViewController
        self.slideMenuController()?.navigationController?.pushViewController(chitdetailVc, animated: true)
    }
}

I am unable to understand the hierarchy of SlideMenuViewController and the viewcontroller stack prints nil always. 我无法理解SlideMenuViewController的层次结构,并且viewcontroller堆栈始终显示nil。

I assume that you are using https://github.com/dekatotoro/SlideMenuControllerSwift . 我假设您正在使用https://github.com/dekatotoro/SlideMenuControllerSwift However, I don't think that's your issue here. 但是,我认为这不是您的问题。 I think the problem is with your setup of the UINavigationController. 我认为问题出在您的UINavigationController设置上。 Instead of setting the mainViewContoller as a property of the ExSlideViewController , you should use the navigation controller that contains it: 而不是设置mainViewContoller作为的财产ExSlideViewController ,你应该使用包含它的导航控制器:

let slideMenuController = ExSlideMenuController(mainViewController: nvc, leftMenuViewController: menuViewController)

Then, you can push directly in the MainViewController, instead of having to call the parent slideMenuController : 然后,您可以直接在MainViewController中进行推送,而不必调用父slideMenuController

@IBAction func rightHeaderButtonClicked(_ sender: UIBarButtonItem) {
    let chitdetailVc = self.storyboard?.instantiateViewController(withIdentifier: "ChitDetailViewController") as! ChitDetailViewController
    self.navigationController?.pushViewController(chitdetailVc, animated: true)
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Swift 从一个视图控制器导航到另一个视图控制器 - How to Navigate from one View Controller to another using Swift 如何从一个视图控制器导航到另一视图控制器? - How to navigate from one view controller to another view controller? 如何从popoverpresentation视图控制器导航到另一个视图控制器 - How to navigate from popoverpresentation view controller to another view controller 如何在 Swift 中以编程方式从一个控制器导航到另一个视图控制器? - How to navigate from one controller to another View Controller programmatically in Swift? 如何在不使用情节提要或IB的情况下从嵌入式集合视图的单元导航到另一个视图控制器? - How to navigate from an embedded collection view’s cell to another view controller without using Storyboard or IB? 如何在推送通知中导航到另一个视图控制器 - How to navigate to another view controller in push notifications 如何在xcode中从一个表视图控制器导航到另一个表视图控制器 - how to navigate from one table view controller to another table view controller in xcode 如何在按钮单击时从一个视图控制器导航到另一个视图控制器? - How to navigate from one view controller to another view controller on button click? 单击按钮后,如何从视图控制器导航到另一个视图控制器? - How i can navigate from View Controller to another View Controller once clicking on button? 如何在从SlideMenuViewController实例化的ViewControllers中打开TabBarController - How to open a TabBarController in ViewControllers that Instantiate from SlideMenuViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM