John-Lluch / SWRevealViewController

A UIViewController subclass for presenting side view controllers inspired on the FaceBook and Wunderlist apps, done right !
Other
4.52k stars 989 forks source link

Get ViewController of sw_rear? #612

Closed Dbigshooter closed 8 years ago

Dbigshooter commented 8 years ago

Hi there!

My problem seems fairly simple, but I just can't figure out how to solve it. When my MenuViewController is showing, it's the rear one, I need to set a delegate in the MenuViewController so I can modify the content in the ViewController I came from. For instance I have a button in the MenuViewController which is meant to modify the map in the ViewController I am showing the MenuViewController from. Any suggestions? Really appreciated!

iDevelopper commented 8 years ago

Yes subclass SWRevealViewController and set the delegate to this subclass. Otherwise, see also https://github.com/iDevelopper/PBRevealViewController as SW is not maintained,anymore!

Dbigshooter commented 8 years ago

Thanks! Could you provide an example for SWRevealViewController? I am having trouble getting a hold of my MenuViewController, when I press the UIItemBarButton on my MapViewController, which is the sw_front. I tried to do this, however it didn't work.

func test() {
        self.revealViewController().revealToggle(self)
        if let viewControllers = self.view.window?.rootViewController?.childViewControllers {
            for viewController in viewControllers {
                // some process
                print(viewController)
                if viewController.isKindOfClass(MenuViewController) {
                    print("yes it is")
                    let c = viewControllers as! MenuViewController
                    c.delegate = self
                }
            }
        }
    }

Do you have guide to how to use PBRevealViewController in Swift?

iDevelopper commented 8 years ago

The class is documented in PBTevealViewController.h and on cocoapods. Use it as SWRevealViewController. On the github repo there is 2 examples written in Swift. Just use pb_left, pb_main and pb_right instead of sw_rear, sw_front and sw_right.

For your problem the rear view controller is: revealViewController().rearViewController. If it is embedded in a navigation controller, the MenuViewController is the topViewController of the navigation controller.

But what is for the MenuViewController's delegate?

Dbigshooter commented 8 years ago

My hierarchy looks like this:

SWRevealViewController --- sw_rear ---> MenuViewController SWRevealViewController --- sw_front ---> MapViewController

When I click a button in the menu, I want to show something on the map in the MapViewController, hence the delegate.

iDevelopper commented 8 years ago

You don't need a delegate to. In your MenuViewController, have you retained a reference to your MapViewController?

Dbigshooter commented 8 years ago

No, how would I do that?

iDevelopper commented 8 years ago

Can you show a screenshot of your app?

Dbigshooter commented 8 years ago

Of the storyboard?

iDevelopper commented 8 years ago

If you don't have a reference for your MapViewController, a new instance is created each time you open it from the menu?

Dbigshooter commented 8 years ago

The only thing I do is this:

self.revealViewController().revealToggle(self)

iDevelopper commented 8 years ago

You have only one front view controller?

iDevelopper commented 8 years ago

Yes the storyboard.

Dbigshooter commented 8 years ago

Initially, yes. You can navigate to other front view's using the menu, but some of the menupoints are meant to manipulate the current viewcontroller you're opening the menu from.

iDevelopper commented 8 years ago

Then when you navigate to others from view and come back to map view, the map reloads.

Dbigshooter commented 8 years ago

Yes.

iDevelopper commented 8 years ago

Then keep a reference to it:

See this sample:

MapSample.zip

Dbigshooter commented 8 years ago

Oh, so you can just do this:

let navController = self.revealViewController().frontViewController as? UINavigationController
mapViewController1 = navController!.topViewController as? MapViewController

And it'll give you the sw_front, meaning the viewcontroller you came from?

iDevelopper commented 8 years ago

You are in my sample? In MenuTableViewController?

mapViewController1 is the reference to the MapViewController instance, you can use it directly.

Dbigshooter commented 8 years ago

Yea, I am. That was exactly what I was looking for, finding a way to get a hold of the instance of mapViewController, so I can do thing with the map inside the controller by clicking on one of the menu points.

iDevelopper commented 8 years ago

Then, it is OK?

Dbigshooter commented 8 years ago

Yea, perfect! Thank you so much!

A last question, when I click the menu item which do stuff on the map, can I make it go back to the map before starting the thing? Since it's a webservice call which can take some time.

iDevelopper commented 8 years ago

Where is called the request?

Dbigshooter commented 8 years ago

It is called in the MapViewController, so I want the menu item to show the mapView and then request.

iDevelopper commented 8 years ago

On button tap:

if the MapViewController is embedded in a navigation controller: let navController = UINavigationController(rootViewController: mapViewController1!) revealViewController().pushFrontViewController(navController, animated:true) else: revealViewController().pushFrontViewController(mapViewController1, animated:true) and call the request function: mapViewController1.request()

iDevelopper commented 8 years ago

I did the same with a marker (added Add a marker in menu):

MapSample 2.zip

Dbigshooter commented 8 years ago

Thanks! Is it done the same way in PBRevealViewController?

iDevelopper commented 8 years ago

Great! Yes. And you can have the left view (menu) above or below the main view.

Dbigshooter commented 8 years ago

Great! Thanks, much appreciated!

Dbigshooter commented 8 years ago

I just have a last question, when I push the map back, and add the pins, they layout I've done, another color on navigationBar is just white, and not the color I want, how come?

iDevelopper commented 8 years ago

I don't understand your question. In your app?

Dbigshooter commented 8 years ago

Nevermind, I figured it out. I had to call the function that setup the layout in viewWillLayoutSubviews, since the viewDidLoad doesn't get called. Is it possible to show on the menu, which menu item I'm current on? If I called the menu from the mapViewController, I could show a grey overlay on the map menu point?

iDevelopper commented 8 years ago

I think it is in your storyboard's constraints for the mapView.

Dbigshooter commented 8 years ago

Can I somehow know which viewController which called the menu?

iDevelopper commented 8 years ago

This is the front view controller.

revealViewController().frontViewController

iDevelopper commented 8 years ago

You can test that with the delegate function: (willMove or didMoveToPosition)

func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) {
    if position == FrontViewPosition.Right { // Menu will open
         ... // Test which front view controller will open the menu
iDevelopper commented 8 years ago

Add this to my sample: In RevealViewController.swift:

func revealController(revealController: SWRevealViewController!, didMoveToPosition position: FrontViewPosition) {
    if position == FrontViewPosition.Right {
        var controller = revealController.frontViewController
        if controller is UINavigationController {
            let nc = controller as! UINavigationController
            controller = nc.topViewController
        }
        print(controller)
    }
}
iDevelopper commented 8 years ago

Is your menu a table view controller? I did not see that you edited a comment above:

"Is it possible to show on the menu, which menu item I'm current on? If I called the menu from the mapViewController, I could show a grey overlay on the map menu point?"

Dbigshooter commented 8 years ago

Yea it is.

I was wondering, can I use SWRevealViewController for my left menu, and PBRevealViewController for my right?

iDevelopper commented 8 years ago

I don't think so! But why use both.

For the table view controller, in viewDidLoad:

    // Uncomment the following line to preserve selection between presentations
    //self.clearsSelectionOnViewWillAppear = false
Dbigshooter commented 8 years ago

I want the left menu to be animated like SWRevealViewController does, and the right like the other.

iDevelopper commented 8 years ago

PBRevealViewController can animates like SW (PBRevealToggleAnimationTypeSpring) and you can provide your custom animation controller too. Did you try the samples?

Dbigshooter commented 8 years ago

Oh perfect, I didn't even notice ;o)

I'll try the samples and get back.

iDevelopper commented 8 years ago

Then in a new project?

Dbigshooter commented 8 years ago

I'm now trying to use PBRevealViewController, but I get a nil here. In the same project as before:

let navController = self.revealViewController().frontViewController as? UINavigationController

And if I try to run the examples I get that there is no podfile and such, 3 errors.

Can PBRevealViewController animate just like SWRevealViewController? Like pushing the main view away and not showing above it?

iDevelopper commented 8 years ago

Where? In the main view controller? Is the view on the screen?

Dbigshooter commented 8 years ago

In the MenuViewController, it crashes since it is nil. But before we continue: Can PBRevealViewController animate just like SWRevealViewController? Like pushing the main view away and not showing above it?

iDevelopper commented 8 years ago

Yes.

Dbigshooter commented 8 years ago

Cool, do I need to subclass it and make my own controller, or can it be done in another way?

And I use static cells in my menu, how can I just change mainview when I click at them?

iDevelopper commented 8 years ago

If you want the main view controller to the right, and below, set leftPresentViewOnTop = false.

Like this?

2016-08-23_16-50-28

Dbigshooter commented 8 years ago

Yea, but without the shadow ;o)

I cannot get the push to work. On my static cell I've made a custom segue with PBRevealViewControllerSeguePushController, but it doesn't work.