【问题标题】:How to navigate from Mainviewcontroller to another view controller using SlideMenuViewcontroller如何使用滑动菜单视图控制器从主视图控制器导航到另一个视图控制器
【发布时间】:2017-10-04 16:44:49
【问题描述】:

单击 rightHeaderButtonClicked 方法时,我试图从 MainViewController 导航到其他 ViewController。但是,它不会导航。我已经分享了下面的代码。

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

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)
    }
}

我无法理解 SlideMenuViewController 的层次结构,并且 viewcontroller 堆栈总是打印 nil。

【问题讨论】:

  • 这里没有足够的信息给你答案。 ExSlideMenuController 上面没有代码。这是你写的,还是开源的?

标签: ios swift uinavigationcontroller


【解决方案1】:

我假设您使用的是https://github.com/dekatotoro/SlideMenuControllerSwift。 但是,我认为这不是您的问题。 我认为问题在于您对 UINavigationController 的设置。 与其将 mainViewContoller 设置为 ExSlideViewController 的属性,不如使用包含它的导航控制器:

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

然后,你可以直接在MainViewController中推送,而不必调用父slideMenuController

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 2014-07-25
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多