iDevelopper / PBRevealViewController

A UIViewController subclass for revealing a left and/or right view controller above or below a main view controller.
MIT License
80 stars 16 forks source link

How to move back from side menu view controllers to tab bar view controllers in swift 3 ? #47

Closed vamsikrishnasurathi closed 6 years ago

vamsikrishnasurathi commented 6 years ago

Here I am using swrevealViewController to display side menu and here I am having tab bar items for view controllers and for this tab bar view controllers will have bar buttons and which will be having action for side menu and here if I select any of the bar buttons in tab bar which will opens side menu and will have access to another view controllers in side menu after going to any view controller then when I press the back button action was working fine for first time and when I move to another view controller in tab bar then also it works fine whenever I move to previous tab bar view controller and later i click side menu view controllers then the view controller was moving in the previous navigation view controller and in the navigation frontNVC the last accessed new view controller was saving in navigation stack can you please help me how to resolve this ?

here is the code which used for moving side menu in did select row delegate method

if cell.titleCellLabel.text! == "Account"
        {
            var controller: UIViewController? = nil
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            controller = storyboard.instantiateViewController(withIdentifier: controllers[indexPath.row])
            print(frontNVC?.viewControllers)
            if controller != nil
            {
                // Prevent stacking the same controller multiple times
                print(frontNVC)
                _ = frontNVC?.popViewController(animated: false)
                frontNVC?.viewControllers.removeAll()
                // Prevent pushing twice FrontTableViewController
                if !(controller is SWRevealViewController) {
                    // Show the controller with the front view controller's navigation controller
                    print(frontNVC)
                    frontNVC!.pushViewController(controller!, animated: false)
                }
                // Set front view controller's position to left
                revealViewController().setFrontViewPosition(.left, animated: true)
            }
        }

Here I am using swrevealViewController to display side menu and here I am having tab bar for five view controllers and in this every view controller need to have bar button and which will be having action for side menu and here if I select any of the bar buttons in tab bar which will opens side menu and will have access to another view controllers in side menu after going to any view controller then when I press the back button action was working fine and when I move to another to another view controller in tab bar after coming back from view controller then also it works fine whenever I move to previous tab bar view controller and click side menu view controllers then the view controller was moving in the previous navigation controller and in the navigation frontNVC the last accessed new view controller was saving in navigation stack so if i select any view controller in side menu when tab item was in home then the view controller was displaying in last selected tab bar item view controller not in the home view controller and first time after running app it was working fine later not works and here i am using tab bar and side menu for displaying Please help me how to resolve this ?

here is the code which used for moving side menu in did select row delegate method

  ```

if cell.titleCellLabel.text! == "Account" { var controller: UIViewController? = nil let storyboard = UIStoryboard(name: "Main", bundle: nil) controller = storyboard.instantiateViewController(withIdentifier: controllers[indexPath.row]) print(frontNVC?.viewControllers) if controller != nil { // Prevent stacking the same controller multiple times print(frontNVC) _ = frontNVC?.popViewController(animated: false) frontNVC?.viewControllers.removeAll() // Prevent pushing twice FrontTableViewController if !(controller is SWRevealViewController) { // Show the controller with the front view controller's navigation controller print(frontNVC) frontNVC!.pushViewController(controller!, animated: false) } // Set front view controller's position to left revealViewController().setFrontViewPosition(.left, animated: true) } }

here is the code used for back button action used in side menu view controllers

@IBAction func backButtonAction( sender: Any) { let nc = revealViewController().rearViewController as? UINavigationController let frontNVC = (nc?.topViewController as? LeftSideViewController)?.frontNVC = frontNVC?.popViewController(animated: true) }


Here is my story board layout image shown below

![screen shot 2018-04-30 at 3 32 01 pm](https://user-images.githubusercontent.com/32094391/39427932-3fc48574-4ca3-11e8-83b3-f26dddd74f50.png)
iDevelopper commented 6 years ago

Sorry but you are wrong. Here, it is PBRevealViewController not SWRevealViewController.

--> https://github.com/John-Lluch/SWRevealViewController

vamsikrishnasurathi commented 6 years ago

Actually the code was posted by using 'SWRevealViewController' but later i tried 'PBRevealViewController' also but the both libraries almost resemble same and posted the question

iDevelopper commented 6 years ago

Where do you want to come back from Account view controller? Always to Home view controller or to the controller that was selected when you pushed the account view controller?

vamsikrishnasurathi commented 6 years ago

any how thanks for reply @iDevelopper i got the required with SWRevealViewController but let me know how to implement the same using PBRevealViewController when selecting back button in rear view controllers to go back to home page or which ever selected page in tab bar in main view controllers

iDevelopper commented 6 years ago

Can the account view controller be able to open the side menu or no? If yes, how? With another button as there is already a back button...

vamsikrishnasurathi commented 6 years ago

No there is no chance to open side menu in my account page only back button will be there to come back to previous view controller

iDevelopper commented 6 years ago

Here is a sample with SW:

SWIssue782 2.zip

If you need some help for PBRevealViewController please open a new issue at https://github.com/iDevelopper/PBRevealViewController

vamsikrishnasurathi commented 6 years ago

I tried to open the project but it didn't open and it shows error @iDevelopper
here is the error displayed screen shot 2018-05-05 at 9 59 14 am How to open this and i was using Xcode 9.2 ?

iDevelopper commented 6 years ago

Try this one:

SWIssue782 2.zip

vamsikrishnasurathi commented 6 years ago

Thanks working perfectly but i also got the same thing with different code is it fine with this or should i need to replace like your code ?

for moving from side menu to another view controller

i had declared the code in left side view controller like this

    var frontNVC: UINavigationController?
    var frontVC: HomeViewController?
    var secondVC : CategoriesViewController?
    var thirdVC : BrandLandingViewController?
    var fourthVC : OffersViewController?
    var fifthVC : CartViewController?
    var loginVC : LogInViewController?  

using below code to move from main view controller to side menu view controllers

            var controller: UIViewController? = nil
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            controller = storyboard.instantiateViewController(withIdentifier: controllers[indexPath.row])
            if controller != nil
            {
                _ = frontNVC?.popViewController(animated: false)

                if !(controller is HomeViewController) {
                    print(frontNVC)
                    frontNVC!.pushViewController(controller!, animated: false)
                }
                revealViewController().setFrontViewPosition(.left, animated: true)
            }

To make view controller to come back to same main view controller i was using this code

           let nc = revealViewController().rearViewController as? UINavigationController 
           let frontNVC = (nc?.topViewController as? LeftSideViewController)?.frontNVC
        _ = frontNVC?.popViewController(animated: true)

And in the same main view controller i am initializing which navigation it was and the code is

       let nc = revealViewController().rearViewController as? UINavigationController    
       (nc?.topViewController as? LeftSideViewController)?.frontNVC = self.navigationController  
       (nc?.topViewController as? LeftSideViewController)?.frontVC = self
iDevelopper commented 6 years ago

It seems more complicated. Your frontNVC is the HomeVC navigation controller. What happen if you push from another tab? Where do you use your variable frontVC, secondVC, thirdVC etc... as you instantiate your controllers from storyboard each time?

vamsikrishnasurathi commented 6 years ago

if i push from another tab i will make the selected tab navigation code as shown in above code will come back and i am using the selected view controller variable here

let nc = revealViewController().rearViewController as? UINavigationController    
       (nc?.topViewController as? LeftSideViewController)?.frontNVC = self.navigationController  
       (nc?.topViewController as? LeftSideViewController)?.frontVC = self

any how your code was simple and works fine will try to change thanks for reply @iDevelopper