2017-10-04 51 views
0

我試圖從Mainviewcontroller導航到其他視圖控制器,當點擊rightHeaderButtonClicked method.But,它不會導航。我已經分享了下面的代碼。如何使用SlideMenuViewcontroller從Mainviewcontroller導航到另一個視圖控制器

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的層次結構和視圖 - 控制堆棧版畫始終爲零。

+0

這裏沒有足夠的信息給你一個答案。上面沒有用於ExSlideMenuController的代碼。這是你寫的東西,還是開源的東西? – Stephen

回答

0

我假定您使用的是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) 
} 
相關問題