slideMenuController无法正常工作

时间:2015-09-25 12:13:49

标签: swift

我正在使用github的slideMenuController来实现左抽屉效果 我成功实施了。
但我有一个问题,从菜单我导航到viewController2和viewController2有一个uibutton推送viewcontroller1。
现在我从slideMenuController中选择viewcontroller2,但它导航到viewcontroller1而不是viewcontroller2。
我在appdelegate中有导航

let storyboard = UIStoryboard(name: "Main", bundle: nil)

    var mainViewController : MainViewController = storyboard.instantiateViewControllerWithIdentifier("MainViewController") as MainViewController
    let leftViewController = storyboard.instantiateViewControllerWithIdentifier("LeftViewController") as LeftViewController
    let rightViewController = storyboard.instantiateViewControllerWithIdentifier("RightViewController") as RightViewController

     nvc = UINavigationController(rootViewController: mainViewController)

    leftViewController.mainViewController = nvc

    let slideMenuController = SlideMenuController(mainViewController:nvc!, leftMenuViewController: leftViewController, rightMenuViewController: rightViewController)
    println("\(nvc!.viewControllers.count)")

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

从slideMenus导航这种导航

func changeViewController(menu: LeftMenu) {
    switch menu {
    case .Main:
        self.slideMenuController()?.changeMainViewController(self.mainViewController, close: true)
    case .Swift:
        self.slideMenuController()?.changeMainViewController(self.swiftViewController, close: true)
        break
    case .Java:
        self.slideMenuController()?.changeMainViewController(self.javaViewController, close: true)
        break
    case .Go:
        self.slideMenuController()?.changeMainViewController(self.goViewController, close: true)
        break
    case .NonMenu:
        self.slideMenuController()?.changeMainViewController(self.nonMenuViewController, close: true)
        break
    default:
        break
    }
}


在slideMenuController中的函数

public func changeMainViewController(mainViewController: UIViewController,  close: Bool) {

    removeViewController(self.mainViewController)
    self.mainViewController = mainViewController
    setUpViewController(mainContainerView, targetViewController: mainViewController)
    if (close) {
        closeLeft()
        closeRight()
    }
}


我正在尝试删除appdelegate的导航堆栈,并在更改菜单时再次分配它。但它没有起作用。我能实现吗?请帮帮我。

1 个答案:

答案 0 :(得分:1)

错误出现在JavaViewController中,如下所示: -

class JavaViewController: UIViewController {

 override func viewDidLoad() {
     super.viewDidLoad()
     var btn : UIButton = UIButton(frame: CGRectMake(150, 150, 60, 60))
     btn.setTitle("Click", forState: UIControlState.Normal)
     btn.addTarget(self, action: "btnClick:", forControlEvents: UIControlEvents.TouchUpInside)
     view.addSubview(btn)


 }

 override func viewWillAppear(animated: Bool) {
     super.viewWillAppear(animated)
     self.setNavigationBarItem()
 }

 func btnClick(sender: UIButton) {

     var storyboard = UIStoryboard(name: "Main", bundle: nil)
     let mainViewController = storyboard.instantiateViewControllerWithIdentifier("MainViewController") as! MainViewController
     let leftViewController = storyboard.instantiateViewControllerWithIdentifier("LeftViewController") as! LeftViewController

     let nvc: UINavigationController = UINavigationController(rootViewController: mainViewController)
     leftViewController.mainViewController = nvc
     self.slideMenuController()?.changeMainViewController(nvc, close: true)

 }

}