ykyouhei / KYDrawerController

Side Drawer Navigation Controller similar to Android
MIT License
622 stars 159 forks source link

PopToRootViewController #25

Closed Ritooon closed 6 years ago

Ritooon commented 8 years ago

Hi guys !

It's me again. I tried to put a row in my tableView in the menu to perform a log out. So I do that :

self.navigationController?.popToRootViewControllerAnimated(true)

But it doesn't work. I tried to put a button, in the navigation bar, it worked.

I don't know how to manage :/

EDIT : In fact, I can't access to the navigationController, because when I perform print(self.navigationController?.viewControllers), it returns nil

Cheers

Ritooon commented 8 years ago

I'm using the auto layout mode (Not storyboard), but I still can't manage it ... When I perform a print(self.navigationController?.viewControllers)in my secondViewController, I get all the navigationController. But impossible to access it in the DrawerViewController ... Don't know how to do ...

hardikdevios commented 8 years ago

ok first of all to access the Drawer you just have to write this in your menu class

   if let drawerController = navigationController?.parentViewController as? KYDrawerController {

         if let navigationObj =  drawerController.mainViewController as? UINavigationController {
                //Pop To rootView Here 
         }

   }
i hope this will fix the issue to pop the screen to rootView controller
Ritooon commented 8 years ago

Thanks for your reply.

Maybe, because of my english you misunderstood ... :/ When I open menu with this :

if(drawerController.drawerState.hashValue == 0) { drawerController.setDrawerState(.Closed, animated: true) } else { drawerController.setDrawerState(.Opened, animated: true) }

I have my TableView, and in the method func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) I would like to access to the navigationController.

Ritooon commented 8 years ago

Futhermore, in my file "DrawerViewController", I only access to drawer with this :

if let drawerController = parentViewController as? KYDrawerController { drawerController.setDrawerState(.Closed, animated: true) }

hardikdevios commented 8 years ago

@Ritooon so now print the drawerController.mainViewController , and let me know what its printing ?

Ritooon commented 8 years ago

Hi ! Thank you very much for help.

If I'm logged, FirstViewController automatically push SecondViewController (self.navigationController?.pushViewController(secondViewController, animated: true))

This is what I have when I'm printing drawerController.mainViewController :


File :AppDelegate In : func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool <UINavigationController: 0x7fe37a800000>


File : FristViewController In :\ func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool <UINavigationController: 0x7fe37a800000>


File : SecondViewController In : if let drawerController = navigationController?.parentViewController as? KYDrawerController <UINavigationController: 0x7fe37a800000>


File : DrawerViewController In : if let drawerController = parentViewController as? KYDrawerController <UINavigationController: 0x7fe37a800000>

hardikdevios commented 8 years ago

@Ritooon you have to change the Rootview controller by calling this

let storyboard:UIStoryboard = UIApplication.sharedApplication().delegate.window?.rootViewController!.storyboard
let home = storyboard!.instantiateViewControllerWithIdentifier("firstView") 

         UIView.transitionWithView(UIApplication.sharedApplication().delegate.window!, duration: 0.5, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: { () -> Void in
                   UIApplication.sharedApplication().delegate.window?.rootViewController = home

                        }, completion: nil)
"firstview" will be your main rootview when user is not logged in
Ritooon commented 8 years ago

I'm not using Storyboard .. :/

hardikdevios commented 8 years ago

Yes but you can take your main file with nib code

Ritooon commented 8 years ago

You mean the AppDelegate ? (Where I declare my rootViewController ?)

If yes, this is the code

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let mainViewController = ViewController(nibName: "ViewController", bundle: nil)

    let drawerViewController = DrawerViewController()

    let drawerController = KYDrawerController()

    drawerController.mainViewController = UINavigationController(
        rootViewController: mainViewController
    )

    drawerController.drawerViewController = drawerViewController

    // Style du menu gauche
    drawerController.drawerWidth = 200

    // Status Bar style
    UIApplication.sharedApplication().statusBarStyle = .LightContent

    /* Customize
    drawerController.drawerDirection = .Right
    */

    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window?.rootViewController = drawerController
    window?.makeKeyAndVisible()

    return true
}
zj2050 commented 8 years ago

hi,thanks first

i want to use it with a tabBarController like this

    let drawerViewController = LeftViewController()

    drawerController.mainViewController = UINavigationController(
        rootViewController: configurationRootViewController()
    )

    drawerController.navigationController?.title = "tsing"; 
    drawerController.drawerViewController = drawerViewController

    /* Customize */
    drawerController.drawerDirection = .Right
    drawerController.drawerWidth     = 300

and the configurationRootViewController like

func configurationRootViewController() -> UITabBarController {

    let todayListCtrl = UIStoryboard(name: "tsing", bundle: nil).instantiateViewControllerWithIdentifier("todaySB") as! TsingTaskController
    todayListCtrl.tabBarItem.title = "today"
    todayListCtrl.tabBarItem.image = UIImage(named: "tabbar_me")?.imageWithRenderingMode(.AlwaysOriginal)
    todayListCtrl.tabBarItem.selectedImage = UIImage(named: "tabbar_meHL")?.imageWithRenderingMode(.AlwaysOriginal)
    let todayNavigationController = UINavigationController(rootViewController: todayListCtrl)

    // Create `chatsTableViewController`
    let focusCtrl = UIStoryboard(name: "tsing", bundle: nil).instantiateViewControllerWithIdentifier("focusSB") as! TsingFocusController
    focusCtrl.tabBarItem.title = "focus"
    focusCtrl.tabBarItem.image = UIImage(named: "tabbar_discover")?.imageWithRenderingMode(.AlwaysOriginal)
    focusCtrl.tabBarItem.selectedImage = UIImage(named: "tabbar_discoverHL")?.imageWithRenderingMode(.AlwaysOriginal)
    let focusNavigationController = UINavigationController(rootViewController: focusCtrl)

    let tabBarController = UITabBarController(nibName: nil, bundle: nil)
    tabBarController.viewControllers = [todayNavigationController, focusNavigationController]

    return tabBarController
}

TsingTaskController , TsingFocusController are UITableViewController

the app is run, but draw is not work, can give me some point how make tabBar work, thanks

another thing , how to set drawer title ? drawerController.navigationController?.title = "tsing"; no effect

timusus commented 6 years ago

This is an iOS development question, not a KyDrawerController specific problem.